public class MainActivity extends Activity implements OnClickListener {
private Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt:
showPopWindow(v);
break;
case R.id.layoutSeclect1:
Toast.makeText(context, "模式1", Toast.LENGTH_SHORT).show();
break;
case R.id.layoutSeclect2:
Toast.makeText(context, "模式2", Toast.LENGTH_SHORT).show();
break;
}
}
private void showPopWindow(View v) {
// 一、把一个自定义的布局打气,作为popwindow要显示的内容
View contentView = LayoutInflater.from(context).inflate(
R.layout.pop_layout, null);
// 二、找到该布局中的控件
LinearLayout layoutSeclect1 = (LinearLayout) contentView
.findViewById(R.id.layoutSeclect1);
LinearLayout layoutSeclect2 = (LinearLayout) contentView
.findViewById(R.id.layoutSeclect2);
// 三、设置控件的点击事件
layoutSeclect1.setOnClickListener(this);
layoutSeclect2.setOnClickListener(this);
// 四、new出PopupWindow,参数为PopupWindow显示的内容view、宽、高、是否可获取焦点
PopupWindow popupWindow = new PopupWindow(contentView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);// 不写也行,因为默认为true,可触摸
// 五、★必须设置背景:如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(new BitmapDrawable(context
.getResources()));
// ——————————————————————————————————————————————————————————————————————————————————————————
// 六、让PopupWindow显示出来:显示popwindow的方式有两种,看你是想相对于某个控件显示,还是相对于父控件显示
// 1.showAsDropDown指的是显示在v控件的周围,是以这个控件为基点的
// popupWindow.showAsDropDown(v);
// popupWindow.showAsDropDown(v, 0, -30);//这里的坐标是基于附着控件的左下角,下右为正
// 2.showAtLocation指的是显示在现对于父控件的位置,第一个参数view和相对于控件的一样
popupWindow.showAtLocation(v, Gravity.CENTER | Gravity.CENTER, 0, 0);
}
}
布局
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mypopwindowdemo.MainActivity" >
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="模式选择" />
</RelativeLayout>
PopupWindow布局
<?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="#0000" >
<!--
popupwimdow的布局注意不能使用类似
android:layout_marginTop=""这样属性,特别是相对布局时候.
布局或者控件整体靠左等都会影响popupwimdow定位使用
-->
<LinearLayout
android:id="@+id/hot_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pop"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layoutSeclect1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="OnclickTestListener" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="回家的方案1"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layoutSeclect2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="OnclickTestListener" >
<TextView