PopupWindow简介

PopupWindow是一个类似dialog的控件,可以接受任何的view作为下拉列表显示。

用法

这里写图片描述

代码展示

package peng.liu.test;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ImageView;import android.widget.PopupWindow;import android.widget.TextView;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        View root = this.getLayoutInflater().inflate(R.layout.cell,null);        final PopupWindow popupWindow = new PopupWindow(root,280,360);        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {// popupWindow.showAsDropDown(view);                popupWindow.showAtLocation(findViewById(R.id.button), Gravity.CENTER,20,20);            }        });        root.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                popupWindow.dismiss();            }        });    }}
<?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">    <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:orientation="vertical" >    <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" /></LinearLayout>

更多相关文章

  1. Android 代码当中动态改变某个控件的位置
  2. android 获取本机号码及应加的权限 代码
  3. 代码设置RelativeLayout相对位置设置
  4. android瀑布流代码
  5. 【Android】监听SDCard安装和卸载的代码片段(测试通过)
  6. android : 控件在代码中设置属性-setWidth(int pixels)或setHeig
  7. Android字体简介

随机推荐

  1. Android Talker(2)Project Environment
  2. android 开发 RecyclerView 横排列列表布
  3. Android(安卓)的动作、广播、类别等标识
  4. Android(安卓)Instrument自动测试:Wifi_se
  5. Android Jetpack Compose - Button
  6. Android文字转语音TTS离线程序
  7. android自带的base64编码
  8. 记View越界绘制
  9. Android Studio插件爆红,Plugin Error :
  10. Android使用videoview播放res/raw下的视