A. 如何在 android 應用程序中使用自定義主題
在Android中,定義外觀最簡單的方式是直接設置屬性在視圖對象上。由於要對整個應用程序設置樣式,這種方法就顯得不是很方便了。因此,我們可以創建
樣式來綁定視圖屬性。但要注意的是,樣式只能設置在xml里。這意味著,我們在創建/載入視圖的時候,只能一次設置一個樣式。下面是如何在res
/values/styles.xml文件中給TextView設置屬性的示例。
<style name="CustomText" parent="<a href="http" target="_blank" rel="nofollow">@android</a> :style/TextAppearance.Medium">
<item name="android:textSize">20sp</item> <item name="android:textColor">#008</item>
</style>
我們引用@style/CustomText樣式應用在我們的layout.xml文件中。該樣式引用了一個父樣式
@android:style/TextAppearance.Medium。由於開始樣式引自android: 命名空間,那麼我們這個樣式默認也隨android平台。
創建主題綁定樣式
假設我們要改變我們所有TextView的文字大小和顏色,並且不需要明確設置每一個TextView。這是一個非常常見的情況,幸運的
是,Android提供了一個非常強大的主題機制。從本質上講,主題就是樣式資源本身,使用「key」指向具體的樣式。一個視圖對象基於這個key可以查
看到具體的樣式。下面是一個簡單的例子,在res/values/themes.xml文件:
<style name="Theme.MyApp" parent="<a href="http" target="_blank" rel="nofollow">@android</a> :style/Theme.Holo">
<item name="android:textAppearance">@style/CustomText</item>
</style>
如上所述,主題是樣式資源本身,所以我們聲明一個Theme.App繼承自Android平台提供的holo主題。我們將我們的CustomText樣式
指定到android:textAppearance屬性。屬性就是一個預定義的「變數」,它可以被其他資源元素引用。事實上,它也可以創建自定義屬性在
res/values/attr.xml文件。
現在有趣的事情來了。我們的主題Theme.MyApp不僅可以通過AndroidManifest.xml的設置應用到Activity上,它也可以在
運行時在代碼中設置。你將不得不重新啟動當前Activity(或使用ContextWrapper應用UI的主題部分),但它使主題更加動態。
B. android怎樣優雅的動態改變整個application的theme
Android中實現theme主題可以使用在activity中使用setTheme(int)的方法,SDK中對此方法的說明為:
//Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling android.app.Activity.setContentView or android.view.LayoutInflater.inflate).
//需要在setcontentview函數或者inflate函數之前使用。
效果圖如下:
實現步驟:
首先需要定義一個屬性,此屬性用於賦值給控制項的屬性,相當於控制項屬性值的「變數」。
在attrs.xml中,定義三個屬性,屬性的format均為reference|color
<resources>
<attr name="button_bg" format="reference|color" />
<attr name="activity_bg" format="reference|color" />
<attr name="text_cl" format="reference|color" />
</resources>
接下來,在styles.xml中,編寫自定義的Theme
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="text_cl">#ffffff</item>
<item name="button_bg">#000000</item>
<item name="activity_bg">#ffffff</item>
</style>
<style name="DarkTheme" parent="AppBaseTheme">
<item name="text_cl">#000000</item>
<item name="button_bg">#ffffff</item>
<item name="activity_bg">#000000</item>
</style>
選擇一種模式作為程序的默認theme,注意:由於我是在layout布局文件中定義的view的樣式,因此,為了保證theme切換時不會出現找不到資源的問題,因此需要在每一種用到的自定義theme中,都加上item。這里的item如text_cl和view的textColor屬性的format是一致的。
Android manifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testandroidsettheme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:name="com.example.testandroidsettheme.app.MyApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testandroidsettheme.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
C. Android系統自帶樣式Theme總結
本文從網上復制,給自己做筆記的,摘自: http://blog.csdn.net/hongya1109110121/article/details/11985545
Android系統自帶樣式(@android:style/) (轉)
1 android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"
布局頁面最上面 不會顯示 android:icon="@drawable/ic_launcher"中的值和android:label="@string/app_name"的值。
2 android:theme="@style/AppTheme"
布局頁面最上面 顯示 android:icon="@drawable/ic_launcher"中的值和android:label="@string/app_name"的值。
其他
在AndroidManifest.xml文件的activity中配置
1、android:theme="@android:style/Theme"
默認狀態,即如果theme這里不填任何屬性的時候,默認為Theme
2、android:theme="@android:style/Theme.NoDisplay"
任何都不顯示。比較適用於只是運行了activity,但未顯示任何東西
3、android:theme="@android:style/Theme.NoTitleBar「
背景主題的沒有標題欄的樣式,默認如果沒有設置的話,顯示黑背景
4、android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
背景主題的沒有標題欄且全屏的樣式,默認為黑背景
5、android:theme="@android:style/Theme.Black"
默認狀態下黑背景
6、android:theme="@android:style/Theme.Black.NoTitleBar"
黑背景主題的沒有標題欄的樣式
7、android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
黑背景主題的沒有標題欄且全屏的樣式
8、android:theme="@android:style/Theme.Light"
默認狀態下亮背景,與上述黑背景Theme.Black相反
9、android:theme="@android:style/Theme.Light.NoTitleBar"
亮背景主題的沒有標題欄的樣式,與Theme.Black.NoTitleBar相反
10、android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
亮背景主題的沒有標題欄且全屏顯示的樣式,與Theme.Black.NoTitleBa.Fullscreenr相反
11、android:theme="@android:style/Theme.Dialog"
對話框樣式 將整個activity變成對話框樣式出現
12、android:theme="@android:style/Theme.InputMethod"
Window animations that are applied to input method overlay windows
13、android:theme="@android:style/ Theme.Panel"
刪除掉所有多餘的窗口裝飾,在一個空的矩形框中填充內容,作用范圍相當於把dialog中的所有元素全部去掉,只是一個空的矩形框,且此為默認的樣式
14、android:theme="@android:style/ Theme.Light.Panel"
刪除掉所有多餘的窗口裝飾,在一個空的矩形框中填充內容,作用范圍相當於把dialog中的所有元素全部去掉,只是一個空的矩形框,且默認是light的樣式
15、android:theme="@android:style/Theme.Wallpaper"
使用牆紙做主題,默認狀態。
16、android:theme="@android:style/ Theme.WallpaperSettings"
使用牆紙做主題,默認是使用將上一個界面調暗之後作為主題
17、android:theme="@android:style/ Theme.Light.WallpaperSettings"
使用牆紙做主題,默認Light狀態
18、android:theme="@android:style/Theme.Wallpaper.NoTitleBar"
使用牆紙做主題,且沒有標題欄
19、android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
使用牆紙做主題,且沒有標題欄,且全屏顯示
20、android:theme="@android:style/Theme.Translucent"
半透明狀態下的背景,將運行此activity之前的屏幕作為半透明狀態作為此activity運行時的樣式。
21、android:theme="@android:style/Theme.Translucent.NoTitleBar"
半透明狀態下沒有標題欄的背景,將運行此activity之前的屏幕作為半透明狀態作為此activity運行時的樣式。
22、android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
半透明狀態下沒有標題欄且全屏的背景,將運行此activity之前的屏幕作為半透明狀態作為此activity運行時的樣式。
摘自: http://blog.csdn.net/hongya1109110121/article/details/11985545
D. android選用apptheme,怎樣全屏
如果你想讓這個app啟動的時候,是全屏顯示的話,你可以這樣做:
打開工程目錄下的AndroidManifest.xml文件,雙擊後選擇位於Instrumentation右邊的AndroidManifest.xml選項框
在<activity>節點下找到類似android:name=".MainActivity"的代碼
在該段代碼的下一行加上如下代碼即可:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
最終的效果如下:
E. 如何在 Android 應用程序中使用自定義主題
在Android中,定義外觀最簡單的方式是直接設置屬性在視圖對象上。由於要對整個應用程序設置樣式,這種方法就顯得不是很方便了。因此,我們可以創建 樣式來綁定視圖屬性。但要注意的是,樣式只能設置在xml里。這意味著,我們在創建/載入視圖的時候,只能一次設置一個樣式。下面是如何在res /values/styles.xml文件中給TextView設置屬性的示例。 <style name="CustomText" parent="<a href="http" target="_blank" rel="nofollow">@android</a> :style/TextAppearance.Medium"> <item name="android:textSize">20sp</item> <item name="android:textColor">#008</item> </style> 我們引用@style/CustomText樣式應用在我們的layout.xml文件中。該樣式引用了一個父樣式 @android:style/TextAppearance.Medium。由於開始樣式引自android: 命名空間,那麼我們這個樣式默認也隨android平台。 創建主題綁定樣式 假設我們要改變我們所有TextView的文字大小和顏色,並且不需要明確設置每一個TextView。這是一個非常常見的情況,幸運的 是,Android提供了一個非常強大的主題機制。從本質上講,主題就是樣式資源本身,使用「key」指向具體的樣式。一個視圖對象基於這個key可以查 看到具體的樣式。下面是一個簡單的例子,在res/values/themes.xml文件: <style name="Theme.MyApp" parent="<a href="http" target="_blank" rel="nofollow">@android</a> :style/Theme.Holo"> <item name="android:textAppearance">@style/CustomText</item> </style> 如上所述,主題是樣式資源本身,所以我們聲明一個Theme.App繼承自Android平台提供的holo主題。我們將我們的CustomText樣式 指定到android:textAppearance屬性。屬性就是一個預定義的「變數」,它可以被其他資源元素引用。事實上,它也可以創建自定義屬性在 res/values/attr.xml文件。 現在有趣的事情來了。我們的主題Theme.MyApp不僅可以通過AndroidManifest.xml的設置應用到Activity上,它也可以在 運行時在代碼中設置。你將不得不重新啟動當前Activity(或使用ContextWrapper應用UI的主題部分),但它使主題更加動態。
F. Android 的style和theme
例子
Theme是針對窗體級別的,改變窗體樣式;
Style是針對窗體元素級別的,改變指定控制項或者Layout的樣式
簡單的說就是Theme裡面有包含了好多好多Style
常用於Toolbar下
比如
android:theme="@style/AppTheme.AppBarOverlay設置了Toolbar的主題
app:popupTheme="@style/AppTheme.PopupOverlay"設置了Toolbar節點下的view的主題
默認情況下我們使用theme為android:Theme.Holo.Light.DarkActionBar那麼ActionBar文字是白的,ActionBar Overflow彈出的是黑底白字,如果需求是白底黑字那麼只要設置toolbar的app:popupTheme="ThemeOverlay.AppCompat.Light"
附上一張官方的
裝載自 關於Android的style和theme
G. 怎麼設置android背景顏色
通常來說,每個界面都對應一個activity。而在activity的View視圖中,可以在最外層容器去設置背景圖片或背景顏色。
在xml布局裡:
android:background="@drawable/img1"
或者
android:background="@color/white"
在java代碼里,也可以設置
1 layout.setBackgroundColor(R.color.white);
2 layout.setBackgroundDrawable(drawable);
3 layout.setBackgroundResource(R.drawable.img1);
再者,系統默認的背景色是能過theme來控制的,就是說創建一個activity的背景色,如果在
AndroidManifest.xml文件里有設置如下:
android:theme="@android:style/Theme"
這樣設置activity的主題樣式,"@android:style/Theme"一般是系統默認的。這個不單是背景色,還有其它的樣式,具體可以在網上查一下android:theme的用法。
而"@android:style/Theme"的背景色就是黑色。
H. android 內置theme有哪些
Android系統中自帶樣式分享,需要的朋友可以參考下
android:theme="@android:style/Theme.Dialog" : Activity顯示為對話框模式
android:theme="@android:style/Theme.NoTitleBar" : 不顯示應用程序標題欄
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" : 不顯示應用程序標題欄,並全屏
android:theme="Theme.Light ": 背景為白色
android:theme="Theme.Light.NoTitleBar" : 白色背景並無標題欄
android:theme="Theme.Light.NoTitleBar.Fullscreen" : 白色背景,無標題欄,全屏
android:theme="Theme.Black" : 背景黑色
android:theme="Theme.Black.NoTitleBar" : 黑色背景並無標題欄
android:theme="Theme.Black.NoTitleBar.Fullscreen" : 黑色背景,無標題欄,全屏
android:theme="Theme.Wallpaper" : 用系統桌面為應用程序背景
android:theme="Theme.Wallpaper.NoTitleBar" : 用系統桌面為應用程序背景,且無標題欄
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" : 用系統桌面為應用程序背景,無標題欄,全屏
android:theme="Theme.Translucent : 透明背景
android:theme="Theme.Translucent.NoTitleBar" : 透明背景並無標題
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" : 透明背景並無標題,全屏
android:theme="Theme.Panel ": 面板風格顯示
android:theme="Theme.Light.Panel" : 平板風格顯示
I. android l theme怎麼用
1,用 android l的sdk
2,在res/values/styles.xml 里邊聲明 (具體網路一下)
J. android theme 和stytle的區別
Theme 和Style資源,將一些通用的屬性提取出來,方便使用。
相同:
Theme和Style位於values文件夾下styles.xml下,格式相同。
[java] view plain
<style name="MyTransparent">
<item name="android:windowBackground">@color/transparent_background</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
<item name="Android:windowNoTitle">?android:windowIsTranslucent</item>其中 問號表示引用本主題中已經定義過的資源的值。
在需要使用的時候調用
[java] view plain
<EditText
android:id="@+id/nameEdit"
<span style="color:#ff0000;"> style="@style/LoginOrRegisterEditStyle"</span>
android:hint="@string/input_real_name"
android:layout_below="@id/tipHead"
android:layout_marginTop="20dp" />
不同:
Theme在AndroidManifest.xml中德Application節點或者Activity節點設置android:theme,或者在對應Activity中通過代碼設置setTheme(),影響整個應用或者一個Activity.
Style一般是在布局中的View中設置。影響單個View,如EditText,TextView等等。
如果主題和樣式屬性有沖突,樣式的屬性高於主題。
如果需要使用系統自帶的主題
[java] view plain
android:theme=<span style="color:#ff0000;">"@android:style</span>/Theme.Light.NoTitleBar"