【Android】 修改系统的dialog样式

转自:http://blog.csdn.net/djy1992/article/details/48542645


android 修改系统的dialog样式

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

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

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

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

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

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

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


弹出层方法:

  1. Intent intent=new Intent(Detail_Goods.this,LoginDialog.class);  
  2. tartActivity(intent);  




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

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

其中

  1. setTextColor<pre name="code" class="java">setBackgroundColor  
  1. <strong><span style="color:#990000;">改为自定义颜色代码即可。</span></strong>  
  1. </pre><pre name="code" class="java"/** 
  2.      * AlertDialog 样式 
  3.      * @author:dujinyang 
  4.      */  
  5.     public Dialog setAlertDialogStyle(AlertDialog.Builder alertDialogs) {  
  6.         Dialog dialog = alertDialogs.show();  
  7.         Context context = dialog.getContext();  
  8.         int themeColor = getResources().getColor(R.color.theme_color);  
  9.         int msgColor = getResources().getColor(R.color.medium_gray);  
  10.         final int titleDivider = context.getResources().getIdentifier("titleDivider""id""android");  
  11.         View titleDividerImg = dialog.findViewById(titleDivider);  
  12.         titleDividerImg.setVisibility(View.VISIBLE);  
  13.         titleDividerImg.setBackgroundColor(themeColor);  
  14.         final int contentPanel = context.getResources().getIdentifier("contentPanel""id""android");  
  15.         LinearLayout contentPanelLayout = (LinearLayout) dialog.findViewById(contentPanel);  
  16.         contentPanelLayout.setVisibility(View.VISIBLE);  
  17.         final int message = context.getResources().getIdentifier("message""id""android");  
  18.         TextView messageTextView = (TextView) dialog.findViewById(message);  
  19.         messageTextView.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, context.getResources().getDisplayMetrics()));  
  20.         messageTextView.setTextColor(msgColor);  
  21.         messageTextView.setPadding(12121212);  
  22.         messageTextView.setVisibility(View.VISIBLE);  
  23.         final int title = context.getResources().getIdentifier("alertTitle""id""android");  
  24.         TextView tvTitle = (TextView) dialog.findViewById(title);  
  25.         tvTitle.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, context.getResources().getDisplayMetrics()));  
  26.         tvTitle.setPadding(16161616);  
  27.         tvTitle.setVisibility(View.VISIBLE);  
  28.         tvTitle.setTextColor(themeColor);  
  29.         final int button1 = context.getResources().getIdentifier("button1""id""android");  
  30.         Button negativeButton = (Button) dialog.findViewById(button1);  
  31.         //        negativeButton.setBackgroundResource(R.drawable.button_selector);  
  32.         negativeButton.setVisibility(View.VISIBLE);  
  33.         negativeButton.setTextColor(themeColor);  
  34.         final int button2 = context.getResources().getIdentifier("button2""id""android");  
  35.         Button positiveButton = (Button) dialog.findViewById(button2);  
  36.         //        positiveButton.setBackgroundResource(R.drawable.button_selector);  
  37.         positiveButton.setVisibility(View.VISIBLE);  
  38.         positiveButton.setTextColor(getResources().getColor(R.color.theme_color));  
  39.         final int button3 = context.getResources().getIdentifier("button3""id""android");  
  40.         Button positiveButton2 = (Button) dialog.findViewById(button3);  
  41.         //        positiveButton2.setBackgroundResource(R.drawable.button_selector);  
  42.         positiveButton2.setVisibility(View.VISIBLE);  
  43.         positiveButton2.setTextColor(themeColor);  
  44.         return dialog;  
  45.     }  


三、关于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 indentify= getResources().getIdentifier("icon""drawable""org.anddev.android.testproject");

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


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

  1. static int getResourceId(Context context,String name,String type,String packageName){  
  2.         Resources themeResources=null;  
  3.         PackageManager pm=context.getPackageManager();  
  4.         try {  
  5.             themeResources=pm.getResourcesForApplication(packageName);  
  6.             return themeResources.getIdentifier(name, type, packageName);  
  7.         } catch (NameNotFoundException e) {  
  8.   
  9.             e.printStackTrace();  
  10.         }  
  11.         return 0;  
  12.  }  



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


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

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


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




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



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

1、编写一个文本样式。

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

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

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

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

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


3、设置对话框主题。

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

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


4、自定义App的主题。

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

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

5、指定App主题。

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

  1. <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为例,代码如下:

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


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

7、指定AlertDialog的主题。

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

  1. <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,然后设置颜色。代码如下:

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


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


猜你喜欢

转载自blog.csdn.net/sko121/article/details/53318192