【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()方法被調用之後纔可以去調用,否則會報錯。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章