PopupWindow自适应大小

	private void showPopWindow(Context context, View parent) {

		// TODO Auto-generated method stub

		final PopupWindow pw;

		View myView;

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		myView = inflater.inflate(R.layout.pop, null);

		Button pop_OK = (Button) myView.findViewById(R.id.button_ok);

		Button pop_Cancel = (Button) myView.findViewById(R.id.button_cancel);

		final EditText pop_User = (EditText) myView.findViewById(R.id.edittext);

		final EditText pop_Password = (EditText) myView.findViewById(R.id.password);

		pw = new PopupWindow(myView, 500, 200, true);
		pw.setWidth(LayoutParams.WRAP_CONTENT);                
		pw.setHeight(LayoutParams.WRAP_CONTENT);   
		pw.showAtLocation( myView, Gravity.TOP | Gravity.CENTER, 0, 0);

		pop_OK.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				//showPop_PopWindow();

				// TODO Auto-generated method stub

			}


		});

		pop_Cancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				pw.dismiss();//

			}

		});

		pw.showAsDropDown(MyButton);

	}


只要代码中调用以上函数就可以了

 XML文件代码如下 pop.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffff00"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/mytextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/mytextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账号" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="180dip"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码" />

    <EditText
        android:id="@+id/password"
        android:layout_width="180dip"
        android:layout_height="wrap_content"
        android:password="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff00"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定" />

        <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>


 

以上红色部分特别注意:

自适应屏幕

popupWindow.setWidth(LayoutParams.WRAP_CONTENT);               
    popupWindow.setHeight(LayoutParams.WRAP_CONTENT);  

 

view自适应

              LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout linearLayout = new LinearLayout(c);
                linearLayout.setLayoutParams(params);
 
同样的
 pw.showAtLocation(view,Gravity.TOP | Gravity.LEFT, 252, 50);

以上面一句为例:第一个参数是指PopupWindow显示在哪一个View之上.后面三个参数控制PopupWindow显示的位置,此处表明PopupWindow显示在距左上角x252个像素,y50个像素.


 项目下载:http://download.csdn.net/detail/penglijiang/4419203


   

你可能感兴趣的:(android,service,layout,null,button,encoding)