代码块:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
PopupWindow pop;
private RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl=(RelativeLayout)findViewById(R.id.rl);
}
public void click(View view) {
//加载布局文件
View inflate = View.inflate(this, R.layout.pw_layout, null);
TextView tv=(TextView) inflate.findViewById(R.id.tv_1);
TextView tv2=(TextView) inflate.findViewById(R.id.tv_2);
TextView tv3=(TextView) inflate.findViewById(R.id.tv_3);
TextView tv4=(TextView) inflate.findViewById(R.id.tv_4);
tv.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
tv4.setOnClickListener(this);
//创建一个PopupWindow,设置宽高,把布局文件加载过来
pop=new PopupWindow(inflate, AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new ColorDrawable());//给pop设置背景
pop.setOutsideTouchable(true);//设置外部可触摸事件
pop.showAtLocation(rl, Gravity.BOTTOM,0,0);//设置pop显示的位置
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_1:
Toast.makeText(MainActivity.this,"这就是历史记录", Toast.LENGTH_SHORT).show();
break;
case R.id.tv_2:
rl.setBackgroundColor(Color.GRAY);
break;
case R.id.tv_3:
break;
case R.id.tv_4:
break;
}
if(pop.isShowing()){
pop.dismiss();
}
}
}
布局:
<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"
android:id="@+id/rl">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="呼叫弹出窗口"
android:textSize="30sp"
android:onClick="click" />
</RelativeLayout>
Popupwindow布局:
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:drawableTop="@mipmap/emoji_015"
android:text="历史"
android:gravity="center"
android:id="@+id/tv_1"
android:clickable="true"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:drawableTop="@mipmap/emoji_016"
android:text="设置"
android:gravity="center"
android:id="@+id/tv_2"
android:clickable="true"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:drawableTop="@mipmap/emoji_017"
android:text="下载"
android:gravity="center"
android:id="@+id/tv_3"
android:clickable="true"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:drawableTop="@mipmap/emoji_018"
android:text="帮助"
android:gravity="center"
android:id="@+id/tv_4"
android:clickable="true"/>
</LinearLayout>