前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PopUpWindow的使用

PopUpWindow的使用

作者头像
提莫队长
发布2019-03-01 09:42:40
4270
发布2019-03-01 09:42:40
举报
文章被收录于专栏:刘晓杰刘晓杰

PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。

MainActivity.java

代码语言:javascript
复制
public class MainActivity extends Activity {
	private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				View view = getLayoutInflater().inflate(R.layout.popupwindow, null);
				PopupWindow popupWindow = new PopupWindow(view,
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				//setBackgroundDrawable必须要有,不然点击window以外区域不会消失
				popupWindow.setBackgroundDrawable(
						getResources().getDrawable(android.R.drawable.btn_plus));
//				popupWindow.setTouchable(true);
//				popupWindow.setOutsideTouchable(true);
				popupWindow.setFocusable(true);
				popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
			}
		});
    }
}

main.xml

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.popupwindow.MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/click" />

</RelativeLayout>

popupwindow.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@android:string/cancel" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@android:string/ok" />

</LinearLayout>

实现的效果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年03月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档