轉自:http://blog.csdn.net/djy1992/article/details/48542645
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);
- tartActivity(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)方式一
-
第一個參數格式是:包名 + : + 資源文件夾名 + / +資源名;是這種格式 然後其他的可以爲null
-
Resources resources = context.getResources();
int indentify = resources.getIdentifier(org.loveandroid.androidtest:drawable/icon",null,null);
if(indentify>0){
icon = resources.getDrawable(indentify); - }
(2)方式二
-
第一個參數爲ID名,第二個爲資源屬性是ID或者是Drawable,第三個爲包名。
-
Resources resources = context.getResources();
int indentify= getResources().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()方法被調用之後纔可以去調用,否則會報錯。