2015-10-16 25 views
-1

我有創建popup.xml,我想從資產文件夾中加載我的網頁在WebView中。 這裏是我的popup.xml如何在android中的自定義彈出窗口中加載WebView?

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/popup" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/pp" 
android:orientation="vertical" > 

<WebView 
    android:id="@+id/webview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

這裏是我的活動文件 如何加載我的網頁在彈出窗口的WebView? 有沒有解決方法?

public class MainActivity extends ActionBarActivity { 
    Point p; 
    public int a, b, c, d; 
    ImageButton au, gc, cert, busa; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     au = (ImageButton) findViewById(R.id.au); 
     gc = (ImageButton) findViewById(R.id.gc); 
     cert = (ImageButton) findViewById(R.id.cert); 
     busa = (ImageButton) findViewById(R.id.busa); 

     au.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       if (p != null) { 
        showPopup(MainActivity.this, p); 

       } 

      } 
     }); 
    } 

    // Get the x and y position after the button is draw on screen 
    // (It's important to note that we can't get the position in the onCreate(), 
    // because at that stage most probably the view isn't drawn yet, so it will 
    // return (0, 0)) 
    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 

     int[] location = new int[2]; 

     // Get the x, y location and store it in the location[] array 
     // location[0] = x, location[1] = y. 

     // Initialize the Point with x, and y positions 
     p = new Point(); 
     p.x = location[0]; 
     p.y = location[1]; 
    } 

    // The method that displays the popup. 
    private void showPopup(final Activity context, Point p) { 
     int popupWidth = 720; 
     int popupHeight = 380; 

     // Inflate the popup_layout.xml 
     LinearLayout viewGroup = (LinearLayout) context 
       .findViewById(R.id.popup); 
     LayoutInflater layoutInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup); 

     // Creating the PopupWindow 
     final PopupWindow popup = new PopupWindow(context); 
     popup.setContentView(layout); 
     popup.setWidth(popupWidth); 
     popup.setHeight(popupHeight); 
     popup.setFocusable(true); 

     // Some offset to align the popup a bit to the right, and a bit down, 
     // relative to button's position. 
     int OFFSET_X = 130; 
     int OFFSET_Y = 100; 
     // Clear the default translucent background 
     // popup.setBackgroundDrawable(new BitmapDrawable()); 

     // Displaying the popup at the specified location, + offsets. 
     popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y 
       + OFFSET_Y); 

     // Getting a reference to Close button, and close the popup when 
     // clicked. 

    } 
} 

回答

相關問題