Android中还是会经常用到Popupwindow,一种类似于对话框风格的窗口,当然类似于对话框风格也可以用Activity,可以参考:Android中使用Dialog风格弹出框的Activity
一般使用Popupwindow创建对话框风格的窗口只需要两部:
(1)调用Popupwindow的构造函数创建Popupwindow对象,例如
PopupWindow popupWindow = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
(2)调用Popupwindow的showAsDropDown(View v)将Popupwindow作为v组件的下拉组件显示出来;或者调用Popupwindow的showAtLocation()方法将Popupwindow在指定位置显示。两者可以一起设置
下面就要开始动手啦
首先在activity_main.xml布局文件中添加设置按钮,用于当点击这个按钮时弹出Popupwindow-对话框风格的窗口
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/activity_tabhost_height"
android:background="@color/blue">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="探索Popupwindow"
android:textColor="@color/white"
android:textSize="@dimen/acitvity_title_size" />
<TextView
android:id="@+id/tv_set"
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="设置"
android:textColor="@color/white"
android:textSize="@dimen/acitvity_title_size" />
</RelativeLayout>
</LinearLayout>
然后再创建一个叫activity_set.xml的布局文件,用于点击设置按钮时加载将显示对话框风格的窗口的XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:orientation="horizontal"
android:paddingLeft="@dimen/acitvity_margin"
android:paddingRight="@dimen/acitvity_margin">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="上班时间:"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" />
<Button
android:id="@+id/tv_signin_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:text="9:00"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="下班时间:"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" />
<Button
android:id="@+id/tv_signout_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:text="18:00"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:paddingLeft="@dimen/acitvity_margin"
android:paddingRight="@dimen/acitvity_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="公司位置:"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="5dp"
android:text="重新定位"
android:textColor="@color/blue"
android:textSize="@dimen/size_text_medium" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:paddingLeft="@dimen/acitvity_margin"
android:paddingRight="@dimen/acitvity_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="设置管理员:"
android:textColor="@color/grey"
android:textSize="@dimen/size_text_medium" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:src="@mipmap/icon_toright" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:paddingLeft="@dimen/acitvity_margin"
android:paddingRight="@dimen/acitvity_margin">
<Button
android:id="@+id/btn_exit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@color/white"
android:gravity="center"
android:text="关闭popupWindow"
android:textColor="@drawable/selector_text"
android:textSize="@dimen/size_text_medium" />
</RelativeLayout>
</LinearLayout>
接下来就是MainActivity.class程序开始的主布局文件啦
package com.xiaolijuan.popupwindowdome;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private TextView mTvSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvSet = (TextView) findViewById(R.id.tv_set);
mTvSet.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//加载点击设置按钮时将显示对话框风格的窗口的XML布局文件
View root = this.getLayoutInflater().inflate(
R.layout.activity_set, null);
//创建popupWindow对象(对话框窗口)
final PopupWindow popupWindow = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//以下拉的方式显示
popupWindow.showAsDropDown(v);
//将popupWindow显示在制定位置,在这里作为mTvSet组件的下拉组件显示出来
popupWindow.showAtLocation(findViewById(R.id.tv_set),
Gravity.CENTER, 0, 0);
root.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}
其中点击btn_exit按钮时候,使用popupWindow.dismiss()方法将Popupwindow窗口关闭
效果如下图:
那么现在问题来了,我只能通过点击关闭按钮,我才可以关闭这个窗口么?如果我想点击窗口以外的地方即可关闭窗口呢,那么也设置这样两句代码,如下:
//触摸popupwindow外部,使popupWindow可以消失就必须要设置背景
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//不设置默认的就是False
popupWindow.setOutsideTouchable(true);
这两句代码必须设置在Popupwindow的showAsDropDown(View v)方法或者Popupwindow的showAtLocation()方法之前
完整版:
package com.xiaolijuan.popupwindowdome;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private TextView mTvSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvSet = (TextView) findViewById(R.id.tv_set);
mTvSet.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//加载点击设置按钮时将显示对话框风格的窗口的XML布局文件
View root = this.getLayoutInflater().inflate(
R.layout.activity_set, null);
//创建popupWindow对象(对话框窗口)
final PopupWindow popupWindow = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//触摸popupwindow外部,使popupWindow可以消失就必须要设置背景
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//不设置默认的就是False
popupWindow.setOutsideTouchable(true);
//以下拉的方式显示
popupWindow.showAsDropDown(v);
//将popupWindow显示在制定位置,在这里作为mTvSet组件的下拉组件显示出来
popupWindow.showAtLocation(findViewById(R.id.tv_set),
Gravity.CENTER, 0, 0);
root.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}
当然这一功能还可以使用Activity 的OnTouchEvent事件做到,OnTouchEvent指的是Activity 获得事件(即在PopupWindow窗口之外)
Dome下载