dialog.xml如下:
<?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:background="@android:color/white" android:orientation="vertical" > <ListView android:id="@+id/listView" android:layout_width="270dip" android:layout_height="match_parent" > </ListView> </LinearLayout>
listview_item.xml如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center" android:paddingLeft="6dip" android:minHeight="?android:attr/listPreferredItemHeight" android:textSize="15dip" />
main.xml如下:
<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" tools:context=".MainActivity" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="自定义列表对话框" /> </RelativeLayout>
styles.xml如下:
<resources> <style name="AppBaseTheme" parent="android:Theme.Light"></style> <style name="AppTheme" parent="AppBaseTheme"> </style> <style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowContentOverlay">@null</item> </style> </resources>
mainActivity如下:
package c.c.testdialog; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; /** * Demo描述: * 自定义列表对话框(自定义View中采用ListView) * 该例比自定义列表对话框(二)的代码和效果都要好 * 遇到的问题: * 自定义对话框有黑色边框 * 解决办法: * 设置样式(style) * 参考资料: * http://april19880317.blog.51cto.com/1938095/690380 */ public class MainActivity extends Activity { private Button mButton; private View mDialogView; private Dialog mDialog; private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mButton=(Button) findViewById(R.id.button); mButton.setOnClickListener(new ClickListenerImpl()); } private class ClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: // 弹出自定义对话框 showDialog(); break; default: break; } } } private void showDialog(){ String[] items = new String[] { "发送电子邮件", "分享到Twitter", "分享到FaceBook"}; LayoutInflater layoutInflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); mDialogView=layoutInflater.inflate(R.layout.dialog, null); mListView=(ListView) mDialogView.findViewById(R.id.listView); ArrayAdapter<String> adapter= new ArrayAdapter<String>(MainActivity.this, R.layout.listview_item,items); mListView.setAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { switch (arg2) { case 0: System.out.println("------> 发送电子邮件"); mDialog.dismiss(); break; case 1: System.out.println("------> 分享到Twitter"); mDialog.dismiss(); break; case 2: System.out.println("------> 分享到FaceBook"); mDialog.dismiss(); break; default: break; } } }); mDialog=new Dialog(MainActivity.this, R.style.dialog); mDialog.setContentView(mDialogView); mDialog.show(); } }