主要是用PopupWindow实现,PopupWindow一个弹出窗口,可用于显示任意视图。弹出窗口是一个浮动容器,显示在当前活动的顶部。
具体使用看API https://www.android-doc.com/reference/android/widget/PopupWindow.html
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private EditText et_con;
private ImageButton btn;
private PopupWindow popupWindow;
private ListView listView;
private List<String> list;
private RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_con = findViewById(R.id.et_con);
btn = findViewById(R.id.btn);
rl = findViewById(R.id.rl);
//下拉图标点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
initListView();
//弹窗,下拉列表(listview,宽,高)
popupWindow = new PopupWindow(listView, rl.getWidth(), 400);
//点击外部隐藏
popupWindow.setOutsideTouchable(true);
// 设置空的背景, 响应点击事件
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//设置获取焦点
popupWindow.setFocusable(true);
//显示在指定控件下面
popupWindow.showAsDropDown(et_con, 0, 0);
}
});
}
private void initListView() {
listView = new ListView(this);
listView.setDividerHeight(0);
listView.setBackgroundResource(R.drawable.listview_background);
listView.setOnItemClickListener(this);
list = new ArrayList<>();
//创建一些数据
for (int i = 0; i < 20; i++) {
list.add(10086 + i + "");
}
listView.setAdapter(new MyAdatper());
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String tv = list.get(position);
et_con.setText(tv);
popupWindow.dismiss();
}
class MyAdatper extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1;
if (view == null) {
view1 = View.inflate(viewGroup.getContext(), R.layout.spinnerlist, null);
}else {
view1 = view;
}
TextView textView = view1.findViewById(R.id.text);
textView.setText(list.get(i));
view1.findViewById(R.id.del).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(i);
notifyDataSetChanged();
if (list.size() == 0){
popupWindow.dismiss();
}
}
});
return view1;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/rl"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="@drawable/bg">
<EditText
android:id="@+id/et_con"
android:layout_width="170dp"
android:layout_height="50dp"
android:background="@null" />
<ImageButton
android:id="@+id/btn"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@null"
android:src="@drawable/down_arrow" />
</RelativeLayout>
</LinearLayout>
<?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="50dp"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:src="@drawable/user" />
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="9"
android:text="1234"
android:textColor="#000"
android:textSize="20dp" />
<ImageView
android:id="@+id/del"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:padding="-10dp"
android:src="@drawable/delete" />
</LinearLayout>
网友评论