編輯:關於Android編程
android換膚的方法非常多,如果不需要做成開放接口,只是自己的軟件單純支持換膚,或者在framework層做開發,需要根據系統設置修改app的皮膚,那麼就可以使用theme來實現。其優勢是維護和擴展方面,實現起來也很方便,只要使用xml文件提前定義好需要的皮膚,在項目中加入少量代碼就可以實現。
1.添加變量,xml的變量一般定義在attrs.xml文件中,其位於res/values目錄,需要手動添加該文件。這些變量可以定義成Drawable,color,dim等類型。如下:
2然後在res/values/style.xml文件中給這些變量賦值,不同的style可以給該變量賦不同的值,值的類型受attrs影響,如下:
3.實現一個基礎類調用這些style,其他的activity使用BaseActivity
public class BaseActivity extends Activity { public int mTheme = R.style.AppTheme_Default; @Override protected void onCreate(Bundle savedInstanceState) { /* if (savedInstanceState == null) { mTheme = PreferenceHelper.getTheme(this); } else { mTheme = savedInstanceState.getInt("theme"); } */ setTheme(mTheme); super.onCreate(savedInstanceState); } @Override protected void onResume() { super.onResume(); //if (mTheme != PreferenceHelper.getTheme(this)) { // reload(); //} } protected void reload() { Intent intent = getIntent(); overridePendingTransition(0, 0); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(intent); } }
也可以寫一個類供需要換膚的activity調用,如:
附錄:attrs.xml用法:
1.reference:參考某一資源ID。
(1)屬性定義:
(2)屬性使用:
android:layout_width= "42dip"
android:layout_height= "42dip"
android:background= "@drawable/圖片ID"
/>
2.color:顏色值。
(1)屬性定義:
(2)屬性使用:
android:layout_width= "42dip"
android:layout_height= "42dip"
android:textColor= "#00FF00"
/>
3.boolean:布爾值。
(1)屬性定義:
(2)屬性使用:
android:layout_width= "42dip"
android:layout_height= "42dip"
android:focusable= "true"
/>
4.dimension:尺寸值。
(1)屬性定義:
(2)屬性使用:
android:layout_width= "42dip"
android:layout_height= "42dip"
/>
5.float:浮點值。
(1)屬性定義:
(2)屬性使用:
android:fromAlpha= "1.0"
android:toAlpha= "0.7"
/>
6.integer:整型值。
(1)屬性定義:
(2)屬性使用:
xmlns:android= "http://schemas.android.com/apk/res/android"
android:drawable= "@drawable/圖片ID"
android:pivotX= "50%"
android:pivotY= "50%"
android:framesCount= "12"
android:frameDuration= "100"
/>
7.string:字符串。
(1)屬性定義:
(2)屬性使用:
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:apiKey= "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>
8.fraction:百分數。
(1)屬性定義:
(2)屬性使用:
xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@anim/動畫ID"
android:fromDegrees= "0"
android:toDegrees= "360"
android:pivotX= "200%"
android:pivotY= "300%"
android:duration= "5000"
android:repeatMode= "restart"
android:repeatCount= "infinite"
/>
9.enum:枚舉值。
(1)屬性定義:
(2)屬性使用:
xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
>
10.flag:位或運算。
(1)屬性定義:
(2)屬性使用:
android:name= ".StyleAndThemeActivity"
android:label= "@string/app_name"
android:windowSoftInputMode= "stateUnspecified | stateUnchanged | stateHidden">
注意:
屬性定義時可以指定多種類型值。
(1)屬性定義:
(2)屬性使用:
android:layout_width= "42dip"
android:layout_height= "42dip"
android:background= "@drawable/圖片ID|#00FF00"
/>
Android平台定義的主題樣式:
android:theme="@android:style/Theme.Dialog" 將一個Activity顯示為對話框模式
?android:theme="@android:style/Theme.NoTitleBar" 不顯示應用程序標題欄
?android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不顯示應用程序標題欄,並全屏
?android:theme="@android:style/Theme.Light" 背景為白色
?android:theme="@android:style/Theme.Light.NoTitleBar" 白色背景並無標題欄
?android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 白色背景,無標題欄,全屏
?android:theme="@android:style/Theme.Black" 背景黑色
?android:theme="@android:style/Theme.Black.NoTitleBar" 黑色背景並無標題欄
?android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 黑色背景,無標題欄,全屏
?android:theme="@android:style/Theme.Wallpaper" 用系統桌面為應用程序背景
?android:theme="@android:style/Theme.Wallpaper.NoTitleBar" 用系統桌面為應用程序背景,且無標題欄
?android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" 用系統桌面為應用程序背景,無標題欄,全屏
?android:theme="@android:style/Translucent"半透明效果
?android:theme="@android:style/Theme.Translucent.NoTitleBar" 半透明並無標題欄
?android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 半透明效果,無標題欄,全屏
?android:theme="@android:style/Theme.Panel"
Android平台定義了三種字體大小:
"?android:attr/textAppearanceLarge"
"?android:attr/textAppearanceMedium"
"?android:attr/textAppearanceSmall"
新版本似乎添加了一個巨大字體Huge
Android字體顏色:
android:textColor="?android:attr/textColorPrimary"
android:textColor="?android:attr/textColorSecondary"
android:textColor="?android:attr/textColorTertiary"
android:textColor="?android:attr/textColorPrimaryInverse"
android:textColor="?android:attr/textColorSecondaryInverse"
Android的ProgressBar樣式:
分隔符
橫向:
android:layout_height="1dip"
android:background="?android:attr/listDivider"/>
縱向:
android:background="?android:attr/listDivider"/>
CheckBox樣式
類似標題欄效果的TextView
其它有用的樣式
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingRight="?android:attr/scrollbarSize"
android:layout_height="?android:attr/windowTitleSize"
android:background="?android:attr/windowBackground"
修改Activity的標題欄樣式
如在styles.xml中增加
接著再修改AndroidManifest.xml文件,找到要自定義標題欄的Activity,添加上android:theme值,比如:
去掉所有Activity界面的標題欄
修改AndroidManifest.xml
在application標簽中添加android:theme=”@android:style/Theme.NoTitleBar”
http://www.cnblogs.com/bluestorm/archive/2013/03/20/2971742.html
http://blog.sina.com.cn/s/blog_62ef2f14010105vi.html
http://www.krislq.com/2013/04/android_class_change_skin/
http://blog.csdn.net/wsscy2004/article/details/7562909
最近項目上有需求 ,要求狀態欄透明化 。還有需求是拖動狀態欄標題一下的內容,標題欄的顏色要變化 。這裡所謂的既是狀態欄著色,也是我們經常聽到的沉浸式狀態欄,關於沉浸式的稱
安卓驗證碼的簡單實現我們經常在登錄或者注冊的時候要求輸入驗證碼,這裡簡單介紹一下一種方法 效果如下首先是要獲取 隨機的四個字母組合,我這裡是將26個字母存儲到一個數組中,
本文實例講述了Android編程開發之seekBar采用handler消息處理操作的方法。分享給大家供大家參考,具體如下:該案例簡單實現進度條可走,可拖拽的功能,下面請看
關於四大基本組件的一個總結:1> 4大組件的注冊4大基本組件都需要注冊才能使用,每個Activity、service、Content Provider內容提供者都需