android 修改系统的dialog样式

android 修改系统的dialog样式

一、觉得自定义配置文件麻烦?那就来修改系统自定义XML文件来实现修改系统dialog的样式吧。

如果是在XML中样式:首先来说下样式。 

在 Style.xml 文件(如果没有该文件就创建一个XML名为Style.xml) 

<!--重写系统弹出Dialog -->
    <style name="myDialogTheme" parent="android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">false</item> 
        <item name="android:windowNoTitle">true</item><!--除去title-->
        <item name="android:windowContentOverlay">@null</item> 
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowBackground">@null</item><!--除去背景色-->
     </style>

如果想要去掉背景色边框也就去掉了,在你的层中设置背景色就可以了  

第二步在AndroidManifest.xml中在你注册activity中加入android:theme="@style/myDialogTheme" 这个名就是上面的样式名称

<activity android:name=".LoginDialog" android:theme="@style/myDialogTheme" android:screenOrientation="portrait"/>


弹出层方法:

 Intent intent=new Intent(Detail_Goods.this,LoginDialog.class);
startActivity(intent);




二、回到重点,如果更改系统层的dialog样式:(建议不要使用,因为现在定制机太多,很多机型上会出错)

比如说、想改dialog按钮的颜色、dialog标题的颜色、dialog线条的颜色、dialog去掉标题、dialog去掉标题上线条的颜色等等,都可以在这个方法之上使用

其中

setTextColor<pre name="code" class="java">setBackgroundColor

 

<strong><span style="color:#990000;">改为自定义颜色代码即可。</span></strong>
</pre><pre name="code" class="java"> /**
     * AlertDialog 样式
     * @author:dujinyang
     */
    public Dialog setAlertDialogStyle(AlertDialog.Builder alertDialogs) {
        Dialog dialog = alertDialogs.show();
        Context context = dialog.getContext();
        int themeColor = getResources().getColor(R.color.theme_color);
        int msgColor = getResources().getColor(R.color.medium_gray);
        final int titleDivider = context.getResources().getIdentifier("titleDivider", "id", "android");
        View titleDividerImg = dialog.findViewById(titleDivider);
        titleDividerImg.setVisibility(View.VISIBLE);
        titleDividerImg.setBackgroundColor(themeColor);
        final int contentPanel = context.getResources().getIdentifier("contentPanel", "id", "android");
        LinearLayout contentPanelLayout = (LinearLayout) dialog.findViewById(contentPanel);
        contentPanelLayout.setVisibility(View.VISIBLE);
        final int message = context.getResources().getIdentifier("message", "id", "android");
        TextView messageTextView = (TextView) dialog.findViewById(message);
        messageTextView.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, context.getResources().getDisplayMetrics()));
        messageTextView.setTextColor(msgColor);
        messageTextView.setPadding(12, 12, 12, 12);
        messageTextView.setVisibility(View.VISIBLE);
        final int title = context.getResources().getIdentifier("alertTitle", "id", "android");
        TextView tvTitle = (TextView) dialog.findViewById(title);
        tvTitle.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, context.getResources().getDisplayMetrics()));
        tvTitle.setPadding(16, 16, 16, 16);
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setTextColor(themeColor);
        final int button1 = context.getResources().getIdentifier("button1", "id", "android");
        Button negativeButton = (Button) dialog.findViewById(button1);
        //        negativeButton.setBackgroundResource(R.drawable.button_selector);
        negativeButton.setVisibility(View.VISIBLE);
        negativeButton.setTextColor(themeColor);
        final int button2 = context.getResources().getIdentifier("button2", "id", "android");
        Button positiveButton = (Button) dialog.findViewById(button2);
        //        positiveButton.setBackgroundResource(R.drawable.button_selector);
        positiveButton.setVisibility(View.VISIBLE);
        positiveButton.setTextColor(getResources().getColor(R.color.theme_color));
        final int button3 = context.getResources().getIdentifier("button3", "id", "android");
        Button positiveButton2 = (Button) dialog.findViewById(button3);
        //        positiveButton2.setBackgroundResource(R.drawable.button_selector);
        positiveButton2.setVisibility(View.VISIBLE);
        positiveButton2.setTextColor(themeColor);
        return dialog;
    }


三、关于dialog的一些细节问题

1.如果是使用默认的DIALOG,

关于字体大小:

SpannableStringBuilder ssBuilser = new SpannableStringBuilder("Sample");
StyleSpan span = new StyleSpan(Typeface.ITALIC);
ScaleXSpan span1 = new ScaleXSpan(1);
ssBuilser.setSpan(span, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssBuilser.setSpan(span1, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(ssBuilser);
builder.show();

2. 关于dialog去标题和样式问题,一般使用下面两句

 Dialog dialog = new Dialog(act,R.style.MyTheme);
 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


四、dialog的包名和数据库查询?

那么有人会问,dialog使用包名和数据库怎么定义图标呢?其实很简单

使用getIdentifier()方法可以方便的获各应用包下的指定资源ID
主要有两种方法:
(1)方式一 

  1. 第一个参数格式是:包名 + : + 资源文件夹名 + / +资源名;是这种格式 然后其他的可以为null 
  2. Resources resources = context.getResources();
    int indentify = resources.getIdentifier(org.loveandroid.androidtest:drawable/icon",null,null);
    if(indentify>0){
         icon = resources.getDrawable(indentify);
  3. }

(2)方式二

  1. 第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名。  
  2. Resources resources = context.getResources();
    int indentifygetResources().getIdentifier("icon""drawable""org.anddev.android.testproject");

-------如果找到了,返回资源Id,如果找不到,返回0 。


怎么样动态的根据包名去获取呢?我们来封装成一个方法吧。

static int getResourceId(Context context,String name,String type,String packageName){
        Resources themeResources=null;
        PackageManager pm=context.getPackageManager();
        try {
            themeResources=pm.getResourcesForApplication(packageName);
            return themeResources.getIdentifier(name, type, packageName);
        } catch (NameNotFoundException e) {

            e.printStackTrace();
        }
        return 0;
 }



从数据库里读取图片名称,然后调用图片。直接用R.drawable.?无法调用。查了好多地方最后找到了个方法,分享给大家,希望有帮助。一般建议使用第二种


1. 不把图片放在res/drawable下,而是存放在src某个package中(如:com.drawable.resource),这种情况下的调用方法为:

String path = "com/drawable/resource/imageName.png";
InputStream is = getClassLoader().getResourceAsStream(path);
Drawable.createFromStream(is, "src");


2. 如果还是希望直接使用res/drawable中的图片,就需要通过下面的方法了:
假设创建工程的时候,填写的package名字为:com.test.image
int resID = getResources().getIdentifier("imageName", "drawable", "com.test.image");
Drawable image = getResources().getDrawable(resID);




五、修改android修改HOLO对话框风格



andriod中修改对话框的风格,可以通过设置theme来实现,部分元素需要通过Java代码来修改,下面以修改对话框的标题为例说明各步骤。

1、编写一个文本样式。

DIALOG的标题是一个textview,在sytles.xml中,添加如下代码来设置你自己的文本样式:

<pre name="code" class="java">   <style name="DialogWindowTitle">
        <item name="android:textSize">22sp</item>
        <item name="android:textColor">@color/font_dark_grey</item>
    </style>

 
 

2、设置对话框的标题主题。

上面的标题文本并不能直接设置为对话框的标题样式。 我们还需要编写一个表示标题的主题的style,在这里指定标题的文本样式。代码如下:

    <pre name="code" class="java"><style name="DialogWindowTitle.DeviceDefault">
        <item name="android:maxLines">1</item>
        <item name="android:scrollHorizontally">true</item>
        <item name="android:textAppearance">@style/DialogWindowTitle</item>
    </style>

 
 

3、设置对话框主题。

接下来,我们编写我们的对话框主题,在这里指定标题的主题。由于一些属性并不是public的,所以我们需要继承自原来的某个style,代码如下:

   <pre name="code" class="java"> <!--Dialog主题-->
    <style name="Theme.DeviceDefault.Dialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog" >
        <item name="android:windowTitleStyle">@style/DialogWindowTitle.DeviceDefault</item>
    </style>

 
 

4、自定义App的主题。

接下来,我们需要在我们的App theme中指定我们的对话框使用这种主题,所以需要定义一个App theme。同样由于App theme的许多属性并不是public的(比如下面要提到的标题下面的那条蓝线),所以我们要继承自一个原生的style。这里我根据程序需要选择了Theme.Holo.Light.NoActionBar,代码如下:

   <pre name="code" class="java"> <style name="ParkingTheme" parent="@android:style/Theme.Holo.Light.NoActionBar">
        <item name="android:dialogTheme">@style/Theme.DeviceDefault.Dialog</item>
    </style>

 
 

5、指定App主题。

最后一步,我们需要在AndroidManifest.xml文件中,指定我们的app主题。这步很简单,只需要在application标签中指定android:theme的值即可,如下:

      <pre name="code" class="html">  android:theme="@style/ParkingTheme"

 
 
不过这只是指定了Dialog的主题。如果是通过AlertDialog创建出来的对话框,主题还是原来的。所以我们还需要以下步骤。

6、编写AlertDialog主题。

我们无法直接继承系统主题里的AlertDialog的style。如把parent指定为Theme.DeviceDefault.Dialog.Alert,Theme.Holo.Dialog.Alert,Theme.DeviceDefault.Light.Dialog.Alert或Theme.Holo.Light.Dialog.Alert,都会导致编译不过。所以我们需要继承自Dialog的style。在这里我以Theme.Holo.Light.Dialog为例,代码如下:

    <!--AlderDialog主题-->
    <pre name="code" class="html"><style name="Theme.DeviceDefault.Dialog.Alert"  parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowTitleStyle">@style/DialogWindowTitle.DeviceDefault</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
    </style>

 
 
在这里我参考了原生的alertDialog的style,设定了窗口背景为透明,以及windowContentOverlay为null这两个重要属性,否则你会看到在AlertDialog下面还有一层对话框的背景,或者是对话框的背景遮住了所有内容这样的问题存在。

7、指定AlertDialog的主题。

我们需要在第4步所说的自定义的AppTheme中,添加一行代码来指定要使用的AlertDialog的style,代码如下:

 <pre name="code" class="html"><item name="android:alertDialogTheme">@style/Theme.DeviceDefault.Dialog.Alert</item>

 
 

8、修改标题下面的蓝色线。

如果你修改了对话框的主题颜色,那么标题下面的蓝色的线肯定会让你很郁闷。如果对话框较少,你可以选择隐藏标题,然后自定义一个包含了标题的View来设置为对话框的内容。但是如果你的对话框有许多种,而且本来都是可以调用原来的API就来生成的话,要去定义这么多个带标题的view,这样做下来心里肯定是很纠结的。

标题下面的蓝色的线,并不是在Dialog或AlertDialog中设置或通过它们的style中定义的。它是定义在各种风格的dialog的layout当中,然后再在AppTheme里面指定dialog的对应属性。遗憾的是,目前我看到这几个相关属性还不是public的,不能自己设置,所以只有通过Java代码来实现了。

表示这条蓝色的线的叫做titleDivider,我们可以通过getResources()的API来获取它的IP,然后设置颜色。代码如下:

   <pre name="code" class="java"> public static final void dialogTitleLineColor(Dialog dialog, int color) {
        Context context = dialog.getContext();
        int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
        View divider = dialog.findViewById(divierId);
        divider.setBackgroundColor(color);
    }

 
 

这行代码对于自定义的Dialog,可以在setContentView之后调用。但是对于AlertDialog,必须在show()方法被调用之后才可以去调用,否则会报错。


转载请注明地址:http://blog.csdn.net/djy1992/article/details/48542645


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值