Android自定义dialog

最近项目需要做个退出的dialog,记录下自定义dialog的步奏

1、写自定义dialog布局文件,布局dialog的样式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/exit_layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_dash_exitbg"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/main_dash_exit_msg"
            android:textSize="28sp" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center_horizontal" >

        <Button
            android:id="@+id/main_dash_exit_yes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_yes"
            android:text="@string/exit_yes"
            android:textSize="20sp" />

        <Button
            android:id="@+id/main_dash_exit_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/exit_button_margin"
            android:background="@drawable/button_no"
            android:text="@string/exit_no"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

 

2、自定义dialog代码编写

	private void exit() {
		final Dialog dialog = new Dialog(this, R.style.dialog);
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View dialogView = inflater.inflate(R.layout.exit_dialog,
				(ViewGroup) findViewById(R.id.exit_layout_root));
		exitYesBtn = (Button) dialogView.findViewById(R.id.main_dash_exit_yes);
		exitNoBtn = (Button) dialogView.findViewById(R.id.main_dash_exit_no);

		dialog.setContentView(dialogView);
		dialog.show();

		exitYesBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		exitNoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
	}

3、dialog样式文件配置(dialog初始化不设置样式会有背景白框)

<resources>
    <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">false</item>
    </style>
</resources>

ps:样式文件配置的属性意思

<resources>
    <style name="dialog" parent="@android:style/Theme.Dialog">                           
         <item name="android:windowFrame">@null</item><!--Dialog的windowFrame框为无 -->
        <item name="android:windowIsFloating">true</item><!-- 是否漂现在activity上 -->
        <item name="android:windowIsTranslucent">true</item><!-- 是否半透明 -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item><!--  去除黑色边框的关键设置项 -->
        <item name="android:backgroundDimEnabled">false</item><!--屏幕背景是否变暗-->
    </style>
</resources>

 

效果图由于商业性不便透露
 


版权声明:本文为zmpcwl原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。