两种自定义对话框的方法

第一种:使用Activity来实现自定义对话框。

1. 定义根布局是RelativeLayout。

2. 定义主布局在根布局的中间,需要设定属性centerInParent=”true”。

具体见sms_confirm_dialog.xml解析。

代码片段:

public class ConfirmActivity extends BaseActivity {

    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.sms_confirm_dialog);

    }
}

Intent it = new Intent(mContext, ConfirmActivity.class);
startActivityForResult(it, TxrjConstant.REQUEST_CONFIRM);

背景全为黑是怎么回事?

修改setContentView之后背景仍然是黑色。

View view = LayoutInflater.from(this).inflate(R.layout.sms_confirm_dialog, null);
view.getBackground().setAlpha(10);
setContentView(view);

设定theme之后竟然会多了灰色的边框出来,边框内部是黑色填充。

setTheme(android.R.style.Theme_Dialog);

参考:http://blog.csdn.net/tianshuguang/article/details/6898355

在Manifest.xml文件中设定android:theme竟然让黑色背景消失了,只留下黑色的边框。

<activity android:name=".activity.ConfirmActivity"
     android:theme="@android:style/Theme.Dialog"/>

修改android:theme,改为如下,然后就不再出现黑色边框。

<activity android:name=".activity.ConfirmActivity"
     android:theme="@style/dialog_translucent"/>

<style name="dialog_translucent" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@color/translucent_background</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>

虽然实现了对话框。但是点击半透明的位置,window会自动关闭。

查看android对系统样式Theme.Dialog的定义,找到属性windowCloseOnTouchOutside与此有关。

将它的值在dialog_translucent重新设定为false。

<item name="android:windowCloseOnTouchOutside">false</item>

android系统对样式Theme.Dialog的定义。

<style name="Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
    <item name="android:windowBackground">@android:drawable/panel_background</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowCloseOnTouchOutside">@bool/config_closeDialogWhenTouchOutside</item>
    <item name="android:windowActionModeOverlay">true</item>

    <item name="android:colorBackgroundCacheHint">@null</item>
   
    <item name="textAppearance">@android:style/TextAppearance</item>
    <item name="textAppearanceInverse">@android:style/TextAppearance.Inverse</item>

    <item name="textColorPrimary">@android:color/primary_text_dark</item>
    <item name="textColorSecondary">@android:color/secondary_text_dark</item>
    <item name="textColorTertiary">@android:color/tertiary_text_dark</item>
    <item name="textColorPrimaryInverse">@android:color/primary_text_light</item>
    <item name="textColorSecondaryInverse">@android:color/secondary_text_light</item>
    <item name="textColorTertiaryInverse">@android:color/tertiary_text_light</item>
    <item name="textColorPrimaryDisableOnly">@android:color/primary_text_dark_disable_only</item>
    <item name="textColorPrimaryInverseDisableOnly">@android:color/primary_text_light_disable_only</item>
    <item name="textColorPrimaryNoDisable">@android:color/primary_text_dark_nodisable</item>
    <item name="textColorSecondaryNoDisable">@android:color/secondary_text_dark_nodisable</item>
    <item name="textColorPrimaryInverseNoDisable">@android:color/primary_text_light_nodisable</item>
    <item name="textColorSecondaryInverseNoDisable">@android:color/secondary_text_light_nodisable</item>
    <item name="textColorHint">@android:color/hint_foreground_dark</item>
    <item name="textColorHintInverse">@android:color/hint_foreground_light</item>
    <item name="textColorSearchUrl">@android:color/search_url_text</item>

    <item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
    <item name="textAppearanceMedium">@android:style/TextAppearance.Medium</item>
    <item name="textAppearanceSmall">@android:style/TextAppearance.Small</item>
    <item name="textAppearanceLargeInverse">@android:style/TextAppearance.Large.Inverse</item>
    <item name="textAppearanceMediumInverse">@android:style/TextAppearance.Medium.Inverse</item>
    <item name="textAppearanceSmallInverse">@android:style/TextAppearance.Small.Inverse</item>

    <item name="listPreferredItemPaddingLeft">10dip</item>
    <item name="listPreferredItemPaddingRight">10dip</item>
</style>

定义样式的文件所在位置:

android-sdk-windows\platforms\android-16\data\res\values\themes.xml

第二种:使用Dialog来实现自定义对话框。

private void confirm() {
    Dialog dialog = new Dialog(this, R.style.dialog);
    dialog.setContentView(R.layout.sms_confirm_dialog);

    dialog.setCancelable(true);
    dialog.show();
}

在onBackPressed()方法中调用

confirm();

注意:不要执行super.onBackPressed()方法,否则点击back键就返回到上一个界面。

需要在values/styles.xml文件中定义

<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:background">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.6</item>
</style>

 

解析sms_confirm_dialog.xml文件

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#00000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_centerInParent="true" // 在屏幕的中间
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="180dip"
        android:background="#00000000" // 透明的背景
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip">
        <FrameLayout
            android:background="@drawable/dialog_title"
            android:layout_width="match_parent"
            android:layout_height="50dip">
            <TextView
                android:text="alert"
                android:textSize="16dip"
                android:textColor="@color/white"
                android:layout_gravity="center" // TextView控件位于FrameLayout的中间
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"/> // 透明的背景
        </FrameLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:background="@color/white">
            <TextView
                android:text="No valid recipient, do you abort?"
                android:textSize="16dip"
                android:textColor="@color/black"
                android:gravity="center_vertical" // 将文字显示在TextView的垂直方向的中间
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="10dip"
                android:paddingRight="10dip"
                android:background="@color/white"/>
         </LinearLayout>
         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:orientation="vertical"
            android:background="@android:color/transparent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dip" // 使用颜色来定义单色横线
                android:background="#999999"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dip"
                android:orientation="horizontal"
                android:background="@android:color/transparent">
                <TextView
                    android:text="Yes"
                    android:textSize="16dip"
                    android:textColor="@color/black"
                    android:gravity="center" // 将文字显示在TextView的中间
                    android:layout_width="0dip"
                    android:layout_weight="1" // 伸缩充满剩余空间
                    android:layout_height="match_parent"
                    android:background="#CCCCCC"/>
                <TextView
                    android:layout_width="1dip" // 使用颜色来定义单色竖线
                    android:layout_height="match_parent"
                    android:background="#999999"/>
                <TextView
                    android:text="No"
                    android:textSize="16dip"
                    android:textColor="@color/black"
                    android:gravity="center" // 将文字显示在TextView的中间
                    android:layout_width="0dip"
                    android:layout_weight="1" // 伸缩充满剩余空间
                    android:layout_height="match_parent"
                    android:background="#CCCCCC"/>
              </LinearLayout>
          </LinearLayout>
    </LinearLayout>

</RelativeLayout>

posted on 2013-07-18 01:00  勤修  阅读(4317)  评论(0编辑  收藏  举报

导航