先写popupwindow.xml的布局,,一个输入框和一个按钮
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#00ff00" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/edit_01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:hint="请输入用户名" />
- <Button
- android:layout_gravity="center_horizontal"
- android:id="@+id/pop_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="登录" />
- </LinearLayout>
- </RelativeLayout>
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <Button
- android:id="@+id/btn"
- android:onClick="xianShiPop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="登录" />
- </RelativeLayout>
MainActivity中引入popupwindow的布局 设置popupwindow显示的位置
- public class MainActivity extends Activity {
- private View contentView;
- private PopupWindow popupWindow;
- private Button button;
- private View parent;
- private Button pop_btn;
- private EditText edit_01;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button) findViewById(R.id.btn);
- /**
- * popupWindown可以展示一个视图,,,是一个容器,并且悬浮在当前的activity上面
- *
- * contentView, 窗体要显示的视图...需要自己写在布局里面,,,,要把布局转换成view,,,inflate打气筒
- * width, 窗体弹出的宽度 LayoutParams.WRAP_CONTENT....import android.view.ViewGroup.LayoutParams;
- * height窗体弹出的高度
- *
- * 注意...popupWindown的宽度和高度是由代码中设置的宽度和高度来控制的,,,跟布局中没有关系
- */
- contentView = View.inflate(MainActivity.this, R.layout.pop_layout, null);
- //父窗体
- parent = View.inflate(MainActivity.this, R.layout.activity_main, null);
- //通过构造方法创建一个popupWindown
- popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- /**
- * 出现的问题,,,点击周围不消失,,点击返回键直接退出这个activity...里面的editText控件不能输入
- */
- popupWindow.setTouchable(true);//设置窗体可以触摸,,,默认就是true
- popupWindow.setFocusable(true);//让窗体获取到焦点...一般情况下窗体里面的控件都能获取到焦点,但是editText特殊
- popupWindow.setOutsideTouchable(true);//设置窗体外部可以触摸
- popupWindow.setBackgroundDrawable(new BitmapDrawable());//设置背景
- //popupWindown里面的控件怎么去处理?
- pop_btn = (Button) contentView.findViewById(R.id.pop_btn);
- edit_01 = (EditText) contentView.findViewById(R.id.edit_01);
- pop_btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, edit_01.getText().toString(), 0).show();
- //弹出窗体消失
- popupWindow.dismiss();
- }
- });
- }
- //按钮的点击事件
- public void xianShiPop(View view) {
- //显示一个窗体...只做显示的功能
- //popupWindow.showAsDropDown(button);//展示在控件的左下方
- //popupWindow.showAsDropDown(button, -100, 40);
- /**
- * 在父窗体的某个位置展示
- * View parent,
- * int gravity, 重力/方向 Gravity.BOTTOM...Gravity.CENTER
- * int x,
- * int y
- */
- popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
- }
- }