最重要的是这两行代码
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_1, new 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_1, new 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_1, new 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>

附件列表

Dialog 样式 主题 标题 背景 使用【总结】的更多相关文章

  1. 【Android 应用开发】 ActionBar 样式详解 -- 样式 主题 简介 Actionbar 的 icon logo 标题 菜单样式修改

    作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...

  2. 【Android 应用开发】 ActionBar 样式具体解释 -- 样式 主题 简单介绍 Actionbar 的 icon logo 标题 菜单样式改动

    作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...

  3. android 修改系统的dialog样式

    android 修改系统的dialog样式 一.觉得自定义配置文件麻烦?那就来修改系统自定义XML文件来实现修改系统dialog的样式吧. 如果是在XML中样式:首先来说下样式.  在 Style.x ...

  4. Android中关于系统Dialog无法全屏的问题(dialog样式)

    自定义一个Dialog,继承了系统Dialog的样式.这时候会发现,即使布局文件中写的width和height都是match_parent,依然无法达到全屏的效果. 原因是:系统dialog的样式.默 ...

  5. MVC4 jquery 样式 主题 用法(案例)

    MVC4已经自带了jquery,新建的项目,基本上什么都不用添加就可以运行,跑项目.(集成了那么多东西,jquery,modernizr,自带的默认权限,生成的模板,但是缺没有一个统一的文档或者什么去 ...

  6. Andriod中自定义Dialog样式的Activity点击空白处隐藏软件盘(Dialog不消失)

    一.需求触发场景: 项目中需要出发带有EditText的Dialog显示,要求在编辑完EditText时,点击Dilog的空白处隐藏软键盘.但是Dialog不会消失.示例如下: 二.实现方法: 发布需 ...

  7. android中的样式主题和国际化

    一.Android中的样式和主题     1.1样式     样式是作用在控件上的,它是一个包含一个或者多个view控件属性的集合.android style类似网页设计中的css设计思路,可以让设计 ...

  8. IntelliJ IDEA设置主题和背景图片(背景色)

    设置主题以及背景图片 设置代码背景颜色

  9. c# 自定义含有标题的容器控件(标题背景为渐变色)

    1.控件效果图 此效果图中的标题颜色.字号及字体可以在控件属性中设置.标题背景的渐变色及布局内容的背景色也可以在属性中设置. 2.实现的代码(用户控件) public partial class Uc ...

随机推荐

  1. SaltStack配置管理--状态间的关系(六)

    一.include的引用 需求场景:用于含有多个SLS的状态,使用include可以进行多个状态的组合,将安装apache,php,mysql集合在一个sls中 [root@7mini-node1 p ...

  2. keycloak学习

    keycloak 是一个针对Web应用和RESTfull Web API 提供SSO(Single Sign On:单点登陆),它是一个开源软件,源码地址是:https://github.com/ke ...

  3. Ngnix的日志管理和用定时任务完成日志切割

    一.日志管理 先来看看ngnix的配置文件的server段 接下来我们解释一下默认格式的具体意思 #log_format main '$remote_addr(远程IP) - $remote_user ...

  4. OpenStack openvswitch 实践

    先说下我这架构就是2个节点控制节点+计算节点,网络这采用ovs方法没有路由,就是二层打通并且可以多vlan. 网络架构图: eth0网卡走trunk,走多vlan.从dashboard上创建不同的vl ...

  5. poj 2096Collecting Bugs

    题目链接 poj 2096Collecting Bugs 题解 dp[i][j]表示已经找到i种bug,并存在于j个子系统中,要达到目标状态的天数的期望. 显然,dp[n][s]=0,因为已经达到目标 ...

  6. 范浩强treap——可持久化

    当平衡树需要可持久化的时候,意味着我们需要访问以前的某个时间点的平衡树,就要保持以前的树形态不变,新建一个时间戳,构建一棵新的树. 如果用以前的旋转treap可能就不方便做到(又要打时间戳,又要新建节 ...

  7. 利用dll加载漏洞实现远程代码执行

    微软的“不安全dll加载”漏洞涉及Windows XP至Windows 7等多个版本的操作系统.由于Windows存在加载未指明完整路径的dll文件的机制,可能导致用户在使用第三方软件.玩游戏.听音乐 ...

  8. [HihoCoder1169]猜单词

    题目大意: 给你一个数列,问区间[l,r]内与k最接近的数与k的差是多少. 思路: 将数列中的数和询问的数先从小到大排序, 从小到大枚举每个数,如果是数列上的,就加到线段树中, 如果是询问中的,就在线 ...

  9. bzoj 1007 半交平面简化版

    本题就是求半交平面的交包含哪些直线,而且有点特殊(一般的半交平面用双端队列,因为可能转到最开始的直线,但本题不会,所以只需要一端操作就行了). /*************************** ...

  10. python开发_random

    和java中的random()函数一样,在python中也有类似的模块random,即随机数 下面是我做的demo 运行效果: ==================================== ...