Android自定义对话框列表

学习Android,在文件操作时弹出一个对话框作为弹出菜单(习惯叫法  :-)  ):

new AlertDialog.Builder(MyActivity.this)
       .setTitle("标题")
       .setItems(menu,listener)
       .show(); 

 

其中:

menu:

String[] menu={"打开","重命名","删除","复制","剪切","自动重命名"};

 

listener:

OnClickListener listener = new DialogInterface.OnClickListener() {
     @Override
       public void onClick(DialogInterface dialog, int which)
     {

       //TODO 点击项处理 

       

}; 

当menu内容多时,一屏显示不下,看了看间隔比较到,字体也比较大,如果修改得小一些就可以在一屏显示了。

 

改造对话框:

 

List<Map<String,String>> filemenu= new ArrayList<Map<String, String>>();
for(int i=0;i<menu.length;i++){
Map<String,String> m=new HashMap<String,String>();
m.put("id",menu[i]);
filemenu.add(m);
}
SimpleAdapter adapter = new SimpleAdapter(FileManager.this,
(List<Map<String,String>>) filemenu, R.layout.popupmenu,
new String[] { "id"}, new int[] {R.id.txtItem});
   new AlertDialog.Builder(FileManager.this)
       .setTitle(R.string.OptionMenuTitle)
       //通过自定义适配器来显示菜单
       .setAdapter(adapter,listener)

       .show(); 

 

其中R.layout.popupmenu:

 <?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">
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:id="@+id/txtItem"
     android:textColor="@color/blue" 
     android:textSize="24sp"
     >
    </TextView>
  <ListView android:id="@id/android:list" android:drawSelectorOnTop="false" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
</LinearLayout>

只需要修改这个xml布局文件就可以修改弹出项的外观了。 

原文地址:https://www.cnblogs.com/GarfieldTom/p/2022325.html