简介:
在Android中弹出式菜单(以下称弹窗)是使用十分广泛一种菜单呈现的方式,弹窗为用户交互提供了便利。关于弹窗的实现大致有以下两种方式AlertDialog和PopupWindow,当然网上也有使用Activity并配合Dialog主题的方式实现弹窗,有兴趣的朋友也可以去研究一下。对于AlertDialog和PopupWindow两者的最主要区别就是显示的位置问题:
位置是否固定。 AlertDialog在位置显示上是固定的,而PopupWindow则相对比较随意,能够在主屏幕上的任意位置显示
位置是否固定。 AlertDialog在位置显示上是固定的,而PopupWindow则相对比较随意,能够在主屏幕上的任意位置显示
PopupWindow 是弹出窗口,可以用来显示任意一个视图。出现的弹出窗口是一个在当前Activity之上的浮动容器。
PopupWindow常用的构造方法:
- PopupWindow(Context context)
Create a new empty, non focusable popup window of dimension (0,0).参数为上下文,创建一个高宽为0 的没有焦点的PopupWindow
Create a new empty, non focusable popup window of dimension (0,0).无参,创建一个高宽为0 的没有焦点的PopupWindow
- PopupWindow(View contentView)
Create a new non focusable popup window which can display the contentView.参数为要显示的View,即PopupWindow要显示的视图
- PopupWindow(int width, int height)
Create a new empty, non focusable popup window.参数为要显示的View的宽度和高度
- PopupWindow(View contentView, int width, int height)
Create a new non focusable popup window which can display the contentView.参数为要显示的View,宽度和高度
PopupWindow常见方法:
- dismiss()使PopupWindow消失
- getWidth()得到PopupWindow宽度
- getHeight()得到PopupWindow高度
- isOutsideTouchable()设置popupwindow外部可点击
- setAnimationStyle(int animationStyle)为PopupWindow设置动画效果
- setContentView(View contentView)如果不在构造方法中指定要显示的View,则可以使用此方法
- setHeight(int height)设置PopupWindow高度,如果当前的PopupWindow正显示,那么下次显示时才会有效
- setWidth(int width)设置PopupWindow宽度,如果当前的PopupWindow正显示,那么下次显示时才会有效
- showAsDropDown(View anchor)指定PopupWindow显示在控件的左下方
- showAsDropDown(View anchor, int xoff, int yoff)
- showAsDropDown(View anchor, int xoff, int yoff, int gravity)
(和上面的方法一样,虽然多了一个参数,但貌似官方文档上对其描述都一样)
- showAtLocation(View parent, int gravity, int x, int y)
随意指定啦
- setBackgroundDrawable(Drawable background)设置PopupWindow背景,可为NULL
popwindow的使用,点击右边的button显示破噗噗Window ,popupwindow内部添加的是一个ListView
xml布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.luuuzi.demo002.MainActivity">
<LinearLayout
android:layout_width="300dp"
android:layout_height="50dp">
<EditText
android:id="@+id/et_input"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="50dp"
android:layout_height="match_parent"
android:onClick="onClick"
android:background="@drawable/down_arrow"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity代码
package com.example.luuuzi.demo002;
import android.content.Context;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private ListView listView;
private EditText et_input;
private ArrayList<String> datas;
private String tag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tag="MainActivity";
mContext=this;
initView();
}
private void initView() {
et_input = (EditText) findViewById(R.id.et_input);
}
public void onClick(View view){
showPopWindow();
}
private void showPopWindow() {
initListView();
PopupWindow popupWindow= new PopupWindow(listView,et_input.getWidth(),300);
//将数据添加到popupWindow中显示
//显示在指定控件下面,第2和第3个参数是popupwindow相对于et_input的显示位置
popupWindow.showAsDropDown(et_input,0,0);
}
//初始化要显示的内容
private void initListView() {
listView = new ListView(mContext);
datas = new ArrayList<>();
//创建数据
for (int i=0;i<30;i++){
datas.add(1000+i+"");
}
listView.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView==null){
view=View.inflate(mContext,R.layout.item_number,null);
}else {
view=convertView;
}
TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
tv_number.setText(datas.get(position));
return view;
}
}
}
item布局
<?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:gravity="center">
<ImageView
android:id="@+id/iv_hear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/user"/>
<TextView
android:id="@+id/tv_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="123456"/>
<ImageButton
android:id="@+id/ibtn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/delete"/>
</LinearLayout>
效果图