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>
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:paddingTop="10dip" android:paddingBottom="10dip" android:paddingLeft="5dip" android:paddingRight="5dip" android:background="@android:color/white" android:orientation="vertical" > <TextView android:id="@+id/first" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="25sp" android:text="发送电子邮件" /> <TextView android:id="@+id/second" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginTop="10dip" android:textSize="25sp" android:text="分享到Twitter" /> <TextView android:id="@+id/third" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_marginTop="10dip" android:textSize="25sp" android:text="分享到FaceBook" /> </LinearLayout >
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.Button; import android.widget.TextView; /** * Demo描述: * 自定义列表对话框(自定义View中采用几个TextView) * 遇到的问题: * 自定义对话框有黑色边框 * 解决办法: * 设置样式(style) * */ public class MainActivity extends Activity { private Button mButton; private TextView mFirstTextView; private TextView mSecondTextView; private TextView mThiredTextView; private View mDialogView; private Dialog mDialog; @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()); LayoutInflater layoutInflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); mDialogView=layoutInflater.inflate(R.layout.dialog, null); mFirstTextView=(TextView) mDialogView.findViewById(R.id.first); mFirstTextView.setOnClickListener(new ClickListenerImpl()); mSecondTextView=(TextView) mDialogView.findViewById(R.id.second); mSecondTextView.setOnClickListener(new ClickListenerImpl()); mThiredTextView=(TextView) mDialogView.findViewById(R.id.third); mThiredTextView.setOnClickListener(new ClickListenerImpl()); } private class ClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: init(); // 弹出自定义对话框 mDialog=new Dialog(MainActivity.this, R.style.dialog); mDialog.setContentView(mDialogView); mDialog.show(); break; case R.id.first: System.out.println("------> 第一个TextView"); mDialog.dismiss(); break; case R.id.second: System.out.println("------> 第二个TextView"); mDialog.dismiss(); break; case R.id.third: System.out.println("------> 第三个TextView"); mDialog.dismiss(); break; default: break; } } } }
时间: 2024-11-13 09:41:19