2013-02-27 40 views
0

有人可以帮助我。我有一个弹出窗口,我怎么能自动调整它的android设备的每个分辨率,它必须在我的应用程序的中心?如何自动缩放弹出窗口Android

这是我main.java

public void onWindowFocusChanged(boolean hasFocus) { 

    int[] location = new int[2]; 
    Button button = (Button) findViewById(R.id.show_popup); 

    // Get the x, y location and store it in the location[] array 
    // location[0] = x, location[1] = y. 
    button.getLocationOnScreen(location); 

    //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 = 230; 
    int popupHeight = 300; 



    // 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. 


    // Clear the default translucent background 


    // Displaying the popup at the specified location, + offsets. 
    popup.showAtLocation(layout, Gravity.CENTER, 0,0); 



    // Getting a reference to Close button, and close the popup when clicked. 
    Button close = (Button) layout.findViewById(R.id.filter); 
    close.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     popup.getContentView(); 
    } 
    }); 
} 

这是我popup_layout.xml

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

<Button 
android:id="@+id/filter" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:layout_alignParentRight="true" 
android:layout_below="@+id/CheckBox04" 
android:layout_marginTop="103dp" 
android:text="Filter" /> 

<CheckBox 
android:id="@+id/checkBox3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:layout_alignParentTop="true" 
android:layout_marginTop="40dp" 
    /> 

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:layout_alignParentTop="true" 
android:text="Test" 
android:textAppearance="?android:attr/textAppearanceLarge" /> 

<CheckBox 
    android:id="@+id/CheckBox03" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/CheckBox01" 
    /> 

</RelativeLayout> 
+0

您是否听说过代码缩进? – 2013-02-27 10:58:16

+0

是的,我知道,我已经改变它只为了stackoverflow删除标签 – user1853197 2013-02-27 11:07:40

+0

_deleted tabs_ - 并使其非常难以阅读。请将标签重新放入。 – 2013-02-27 11:09:12

回答

0

如果你的android:你的弹出式窗口的布局比重= “中心”,它应该以所有设备为中心