Dialog 样式 主题 标题 背景 使用【总结】

最重要的是这两行代码
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Dialog没有标题,需在setContentView之前设置
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//设置Dialog背景透明效果

MainActivity

public class MainActivity extends ListActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] array = { "不指定Dialog的主题,指定Activity使用系统定义的主题"//
                "指定Dialog使用系统定义的主题"//
                "设置Dialog无标题、背景透明效果"//
                "设置AlertDialog背景透明效果"};
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1new ArrayList<String>(Arrays.asList(array))));
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        switch (position) {
        case 0:
            startActivity(new Intent(this, Activity1.class));
            break;
        case 1:
            startActivity(new Intent(this, Activity2.class));
            break;
        case 2:
            Dialog dialog = new Dialog(this);//直接设置对话框主题
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Dialog没有标题。需在setContentView之前设置,在之后设置会报错
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//设置Dialog背景透明效果
            dialog.setContentView(R.layout.layout_dialog);
            dialog.show();
            break;
        case 3:
            AlertDialog alertDialog = new AlertDialog.Builder(this).setView(R.layout.layout_dialog).create();
            alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//设置Dialog背景透明效果
            alertDialog.show();
            break;
        }
    }
}

Activity1

public class Activity1 extends ListActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] array = { "【Theme_Light】AlertDialog 宽高均会压缩到最小,背景上下部分有多余的黑色",//【不能用】
                "Dialog 对于内容而言,宽高均为设置的值,自带一个标题栏,宽度不会拉伸及填充"//【去掉标题后问题基本不存在了】
                //
                "【Theme_Holo_Light】AlertDialog    高会压缩到最小,宽为固定值,背景无多余的黑色"//【能用,但background圆角大于1dp的话,背景会有点小瑕疵】
                "Dialog 对于内容而言,宽高均为设置的值,自带一个标题栏,宽度会被拉伸为一个固定值"//【去掉标题后问题基本不存在了】
                //
                "【           Theme_DeviceDefault_Light           】\n         设备默认根主题,与手机系统有关"""//
                //
                "【Theme_Material_Light】AlertDialog    高会压缩到最小,宽为固定值,背景无多余的黑色"//【能用,但background圆角大于1dp的话,背景会有点小瑕疵】
                "Dialog 对于内容而言,宽高均为设置的值,自带一个标题栏,宽度会被拉伸为一个固定值"};//【去掉标题后问题基本不存在了】
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1new ArrayList<String>(Arrays.asList(array))));
        setTitle("指定Activity使用系统定义的主题");
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        switch (position) {
        case 0://
        case 1:
            SystemThemeActivity.launch(this, android.R.style.Theme_Light"Theme_Light", position % 2);
            break;
        case 2://
        case 3:
            SystemThemeActivity.launch(this, android.R.style.Theme_Holo_Light"Theme_Holo_Light", position % 2);
            break;
        case 4://
        case 5:
            SystemThemeActivity.launch(this, android.R.style.Theme_DeviceDefault_Light"Theme_DeviceDefault_Light", position % 2);
            break;
        case 6://
        case 7:
            SystemThemeActivity.launch(this, android.R.style.Theme_Material_Light"Theme_Material_Light", position % 2);
            break;
        }
    }
}

Activity2

public class Activity2 extends ListActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] array = { "【AlertDialog.THEME_HOLO_LIGHT】\nAlertDialog 和在Activity中设置此样式时一样",//【能用】,但background圆角大于1dp的话,背景会有点小瑕疵
                "Dialog 效果真奇葩"//【不能用】
                "【AlertDialog.THEME_DEVICE_DEFAULT_】\nAlertDialog 和在Activity中设置此样式时一样"//【能用】,但background圆角大于1dp的话,背景会有点小瑕疵
                "Dialog 效果真奇葩"//【不能用】
                "【Theme_Dialog】\nAlertDialog 效果真奇葩"//【不能用】
                "Dialog 效果和在Activity中设置Theme_Light一样"// 对于内容而言,宽高均为设置的值,自带一个标题栏,宽度不会拉伸及填充【去掉标题后问题基本不存在了】
                "【更改Theme_Dialog部分属性】AlertDialog 效果和在Activity中设置Theme_Light一样"//【不能用】宽高均会压缩到最小,背景上下部分有多余的黑色
                "Dialog 非常完美,大部分自定义Dialog都是这么干的!但是高版本推荐使用DialogFragment"};//【非常完美】
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1new ArrayList<String>(Arrays.asList(array))));
        setTitle("指定Dialog使用系统定义的主题");
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        switch (position) {
        case 0://
        case 1:
            SystemThemeDialogActivity.launch(this, AlertDialog.THEME_HOLO_LIGHT"AlertDialog.THEME_HOLO_LIGHT", position % 2);
            break;
        case 2://
        case 3:
            SystemThemeDialogActivity.launch(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT"AlertDialog.THEME_DEVICE_DEFAULT_LIGHT", position % 2);
            break;
        case 4://
        case 5:
            SystemThemeDialogActivity.launch(this, android.R.style.Theme_Dialog,   "Theme_Dialog ", position % 2);
            break;
        case 6://
        case 7:
            SystemThemeDialogActivity.launch(this, R.style.DialogTheme"更改Theme_Dialog部分属性", position % 2);
            break;
        }
    }
}

SystemThemeActivity

/**
 * 不指定Dialog的主题,指定Activity使用系统定义的主题
 * @author 白乾涛
 */
public class SystemThemeActivity extends Activity implements OnDismissListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        int themeId = getIntent().getIntExtra("themeId", 0);
        String theme = getIntent().getStringExtra("theme");
        int type = getIntent().getIntExtra("type", 0);
        setTheme(themeId);//设定【Activity的主题】要放到调用父类方法之前

        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        textView.setTextColor(0xffff0000);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        textView.setText(theme);
        setContentView(textView);
        
        //未指定【Dialog的主题】时,Dialog默认使用的是系统提供的dialogTheme主题
        if (type == 0) {
            textView.append("         AlertDialog");
            new AlertDialog.Builder(this).setView(R.layout.layout_dialog).setOnDismissListener(this).create().show();
        } else {
            textView.append("         Dialog");
            Dialog dialog = new Dialog(this);
            //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Dialog没有标题。需在setContentView之前设置,在之后设置会报错
            dialog.setContentView(R.layout.layout_dialog);
            dialog.setOnDismissListener(this);
            dialog.show();
        }
    }
    public static void launch(Context context, int themeId, String theme, int type) {
        Intent intent = new Intent(context, SystemThemeActivity.class);
        intent.putExtra("themeId", themeId);
        intent.putExtra("theme", theme);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }
    @Override
    public void onDismiss(DialogInterface dialog) {
        finish();
    }
}

SystemThemeDialogActivity

/**
 * 指定Dialog使用系统定义的主题
 * @author 白乾涛
 */
public class SystemThemeDialogActivity extends Activity implements OnDismissListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int themeId = getIntent().getIntExtra("themeId", 0);
        String theme = getIntent().getStringExtra("theme");
        int type = getIntent().getIntExtra("type", 0);
        TextView textView = new TextView(this);
        textView.setTextColor(0xffff0000);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        textView.setText(theme);
        setContentView(textView);
        //未指定【Dialog的主题】时,Dialog默认使用的是系统提供的dialogTheme主题
        if (type == 0) {
            new AlertDialog.Builder(this, themeId).setView(R.layout.layout_dialog).setOnDismissListener(this).create().show();
        } else {
            Dialog dialog = new Dialog(this, themeId);//直接设置对话框主题
            //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Dialog没有标题。需在setContentView之前设置,在之后设置会报错
            dialog.setContentView(R.layout.layout_dialog);
            dialog.setOnDismissListener(this);
            dialog.show();
        }
    }
    public static void launch(Context context, int themeId, String theme, int type) {
        Intent intent = new Intent(context, SystemThemeDialogActivity.class);
        intent.putExtra("themeId", themeId);
        intent.putExtra("theme", theme);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }
    @Override
    public void onDismiss(DialogInterface dialog) {
        finish();
    }
}

样式

<resources>
    <style name="DialogTheme" parent="@android:style/Theme.Dialog">
        <!-- 是否不显示title,这个是最重要的 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 设置dialog显示区域外部的背景(透明),注意这里指的是dialog根布局的背景,因为本例中dialog的ContentView有圆角,所以圆角外部区域显示这个颜色 -->
        <item name="android:windowBackground">@android:color/holo_red_light</item>
        <!-- 设置dialog的背景(透明),注意这里不仅包含dialog根布局的背景,还包含本例中ImageView圆角外部的背景。此颜色值会覆盖掉windowBackground的值 -->
        <item name="android:background">@android:color/transparent</item>
        <!-- 设置灰度的值,当为1时,界面除了我们的dialog内容是高亮显示之外,其余区域都是黑色的,完全看不到其他内容,系统的默认值是0.5 -->
        <item name="android:backgroundDimAmount">0.5</item>
        <!-- 是否允许背景灰暗,即是否让显示区域以外使用上面设置的黑色半透明背景,设为false时,:backgroundDimAmount的值等价于0 -->
        <item name="android:backgroundDimEnabled">true</item>

        <!-- 是否有遮盖 -->
        <item name="android:windowContentOverlay">@null</item>
        <!-- 设置Dialog的windowFrame框(无) -->
        <item name="android:windowFrame">@null</item>
        <!-- 是否浮现在activity之上,必须设为true,否则自己独立占一个界面,这根本就不像是一个对话框了 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否半透明,貌似没什么卵用 -->
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>

背景

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item><shape>
            <solid android:color="#0f0" />
            <stroke android:width="1dp" android:color="#00f" />
            <corners android:radius="10dp" />
        </shape></item>
</selector>

内容

<?xml version="1.0" encoding="utf-8"?>
<!-- 注意:不同主题下显示效果大不相同 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="180dp"
    android:layout_height="180dp"
    android:background="@drawable/dialog_bg"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <EditText
        android:layout_margin="5dp"
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

附件列表

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值