300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > android 系统自带主题样式及自定义主题样式

android 系统自带主题样式及自定义主题样式

时间:2022-11-22 09:31:59

相关推荐

android 系统自带主题样式及自定义主题样式

From:/dawanganban/article/details/17732701

/bluestorm/archive//07/12/2588724.html

Android系统自带样式(android:theme)(转)

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" : 平板风格显示

还记得在Android菜鸟的成长笔记(3)中我们曾经遇到了一个问题吗?"这个界面和真真的QQ界面还有点不同的就是上面的标题myFirstApp,怎么去掉这个标题呢?",当时我直接在AndroidMainfest.xml中添加了一个属性:

android:theme="@android:style/Theme.NoTitleBar"

可能有的朋友就会迷惑了,为什么添加了这个属性就可以了。这一篇文章将让我们一起翻开Android系统源代码来揭开困扰大家的关于主题使用以及自定义的谜团。

一、样式(Style)与主题(Theme)

在Android的应用的资源文件中有一个style.xml文件,这个文件是干什么用的呢?我们有时候经常需要对某个类型的组件指定大致相似的格式,比如字体、颜色、背景色等,如果我们每次都为某个View组件去重复指定这些属性,这无疑会产生大量的工作,而且还不利于后期的代码修改和维护。而Style就是一个样式的格式,这个格式可以被多个View组件所使用,也可以说是一个样式的集合类,被需要这一类样式集合的View组件所使用。例如我们前面写的QQ登录界面中的登录按钮,我们可以给定义一个样式

<stylename="buttonStyle"><itemname="android:background">@drawable/login_button_nor</item><itemname="android:textColor">@color/buttonTextColor</item></style>

在布局文件中引入样式

<Buttonandroid:layout_width="270dip"android:layout_height="40dip"android:text="@string/login_button"style="@style/buttonStyle"/>

与样式非常相似,主题资源的xml文件通常也放在/res/values目录下,主题相当于整个应用或者某个Activity的样式,换句话说主题是针对窗体级别或整个应用程序的样式。与样式比较,样式是针对窗体内元素的样式。主题的设置有两种方式

(1)在AndroidMainfest.xml中为Activity或者application指定主题

<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@android:style/Theme.NoTitleBar"><activityandroid:name="com.example.myfirstapp.MainActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application>

上面AndroidMainfest.xml代码中给整个应用指定了一个主题,这个主题是没有标题栏的系统主题。

(2)在Activity创建时调用 setTheme方法(必须在setContentView前面调用)来给某个Activity添加主题。

二、剖析主题(Theme)资源

我们先来创建一个工程名字为helloworld,然后打开它的AndroiodMainfest.xml文件

<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"><activityandroid:name="com.example.helloworld.MainActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application>

可以看到我们在创建工程时,已经默认给我们的整个应用添加了一个主题

android:theme="@style/AppTheme"

打开我们资源文件res/values/下面的styles.xml文件,可以看到在样式文件中有一个名字为AppTheme的样式,如下:

<resources><!--Baseapplicationtheme,dependentonAPIlevel.ThisthemeisreplacedbyAppBaseThemefromres/values-vXX/styles.xmlonnewerdevices.--><stylename="AppBaseTheme"parent="android:Theme.Light"><!--ThemecustomizationsavailableinnewerAPIlevelscangoinres/values-vXX/styles.xml,whilecustomizationsrelatedtobackward-compatibilitycangohere.--></style><!--Applicationtheme.--><stylename="AppTheme"parent="AppBaseTheme"><!--AllcustomizationsthatareNOTspecifictoaparticularAPI-levelcangohere.--></style></resources>

我们可以看到,这个AppTheme的样式继承自上面的AppBaseTheme。而AppBaseTheme又继承自系统的一个样式Theme.Light,打开Android系统源代码找到Theme.xml文件中的Theme.Light如下:

<!--Themeforalightbackgroundwithdarktextontop.Setyouractivitytothisthemeifyouwouldlikesuchanappearance.Aswiththedefaulttheme,youshouldtrytoassumelittlemorethanthatthebackgroundwillbealightcolor.--><stylename="Theme.Light"><itemname="windowBackground">@drawable/screen_background_light</item><itemname="colorBackground">@android:color/background_light</item><itemname="colorForeground">@color/bright_foreground_light</item><itemname="colorForegroundInverse">@android:color/bright_foreground_light_inverse</item><itemname="textColorPrimary">@android:color/primary_text_light</item><itemname="textColorSecondary">@android:color/secondary_text_light</item><itemname="textColorTertiary">@android:color/tertiary_text_light</item><itemname="textColorPrimaryInverse">@android:color/primary_text_dark</item><itemname="textColorSecondaryInverse">@android:color/secondary_text_dark</item><itemname="textColorTertiaryInverse">@android:color/tertiary_text_dark</item><itemname="textColorPrimaryDisableOnly">@android:color/primary_text_light_disable_only</item><itemname="textColorPrimaryInverseDisableOnly">@android:color/primary_text_dark_disable_only</item><itemname="textColorPrimaryNoDisable">@android:color/primary_text_light_nodisable</item><itemname="textColorSecondaryNoDisable">@android:color/secondary_text_light_nodisable</item><itemname="textColorPrimaryInverseNoDisable">@android:color/primary_text_dark_nodisable</item><itemname="textColorSecondaryInverseNoDisable">@android:color/secondary_text_dark_nodisable</item><itemname="textColorHint">@android:color/hint_foreground_light</item><itemname="textColorHintInverse">@android:color/hint_foreground_dark</item><itemname="popupWindowStyle">@android:style/Widget.PopupWindow</item><itemname="textCheckMark">@android:drawable/indicator_check_mark_light</item><itemname="textCheckMarkInverse">@android:drawable/indicator_check_mark_dark</item><itemname="gestureOverlayViewStyle">@android:style/Widget.GestureOverlayView.White</item><itemname="expandableListViewStyle">@android:style/Widget.ExpandableListView.White</item><itemname="listViewStyle">@android:style/Widget.ListView.White</item><itemname="listDivider">@drawable/divider_horizontal_bright</item><itemname="listSeparatorTextViewStyle">@android:style/Widget.TextView.ListSeparator.White</item><itemname="progressBarStyle">@android:style/Widget.ProgressBar.Inverse</item><itemname="progressBarStyleSmall">@android:style/Widget.ProgressBar.Small.Inverse</item><itemname="progressBarStyleLarge">@android:style/Widget.ProgressBar.Large.Inverse</item><itemname="progressBarStyleInverse">@android:style/Widget.ProgressBar</item><itemname="progressBarStyleSmallInverse">@android:style/Widget.ProgressBar.Small</item><itemname="progressBarStyleLargeInverse">@android:style/Widget.ProgressBar.Large</item></style>

样式的继承有两种方式,一种是上面看到的parent="",还有一种就是用”."的方式,上面的Theme.Light继承自Theme。这两种继承有什么区别呢?一个相当于咱们类之间的继承extend,另一个相当于内部类,也可以使用外部类的属性和方法。我们再来看看Theme.Light的父类Theme的样式定义。

<stylename="Theme"><itemname="colorForeground">@android:color/bright_foreground_dark</item><itemname="colorForegroundInverse">@android:color/bright_foreground_dark_inverse</item><itemname="colorBackground">@android:color/background_dark</item><itemname="colorBackgroundCacheHint">?android:attr/colorBackground</item><itemname="disabledAlpha">0.5</item><itemname="backgroundDimAmount">0.6</item><!--Textstyles--><itemname="textAppearance">@android:style/TextAppearance</item><itemname="textAppearanceInverse">@android:style/TextAppearance.Inverse</item><itemname="textColorPrimary">@android:color/primary_text_dark</item><itemname="textColorSecondary">@android:color/secondary_text_dark</item><itemname="textColorTertiary">@android:color/tertiary_text_dark</item><itemname="textColorPrimaryInverse">@android:color/primary_text_light</item><itemname="textColorSecondaryInverse">@android:color/secondary_text_light</item><itemname="textColorTertiaryInverse">@android:color/tertiary_text_light</item><itemname="textColorPrimaryDisableOnly">@android:color/primary_text_dark_disable_only</item><itemname="textColorPrimaryInverseDisableOnly">@android:color/primary_text_light_disable_only</item><itemname="textColorPrimaryNoDisable">@android:color/primary_text_dark_nodisable</item><itemname="textColorSecondaryNoDisable">@android:color/secondary_text_dark_nodisable</item><itemname="textColorPrimaryInverseNoDisable">@android:color/primary_text_light_nodisable</item><itemname="textColorSecondaryInverseNoDisable">@android:color/secondary_text_light_nodisable</item><itemname="textColorHint">@android:color/hint_foreground_dark</item><itemname="textColorHintInverse">@android:color/hint_foreground_light</item><itemname="textColorSearchUrl">@android:color/search_url_text</item><itemname="textAppearanceLarge">@android:style/TextAppearance.Large</item><itemname="textAppearanceMedium">@android:style/TextAppearance.Medium</item><itemname="textAppearanceSmall">@android:style/TextAppearance.Small</item><itemname="textAppearanceLargeInverse">@android:style/TextAppearance.Large.Inverse</item><itemname="textAppearanceMediumInverse">@android:style/TextAppearance.Medium.Inverse</item><itemname="textAppearanceSmallInverse">@android:style/TextAppearance.Small.Inverse</item><itemname="textAppearanceSearchResultTitle">@android:style/TextAppearance.SearchResult.Title</item><itemname="textAppearanceSearchResultSubtitle">@android:style/TextAppearance.SearchResult.Subtitle</item><itemname="textAppearanceButton">@android:style/TextAppearance.Widget.Button</item><itemname="candidatesTextStyleSpans">@android:string/candidates_style</item><itemname="textCheckMark">@android:drawable/indicator_check_mark_dark</item><itemname="textCheckMarkInverse">@android:drawable/indicator_check_mark_light</item><!--Buttonstyles--><itemname="buttonStyle">@android:style/Widget.Button</item><itemname="buttonStyleSmall">@android:style/Widget.Button.Small</item><itemname="buttonStyleInset">@android:style/Widget.Button.Inset</item><itemname="buttonStyleToggle">@android:style/Widget.Button.Toggle</item><!--Listattributes--><itemname="listPreferredItemHeight">64dip</item><!--@hide--><itemname="searchResultListItemHeight">58dip</item><itemname="listDivider">@drawable/divider_horizontal_dark</item><itemname="listSeparatorTextViewStyle">@android:style/Widget.TextView.ListSeparator</item><itemname="listChoiceIndicatorSingle">@android:drawable/btn_radio</item><itemname="listChoiceIndicatorMultiple">@android:drawable/btn_check</item><itemname="expandableListPreferredItemPaddingLeft">40dip</item><itemname="expandableListPreferredChildPaddingLeft">?android:attr/expandableListPreferredItemPaddingLeft</item><itemname="expandableListPreferredItemIndicatorLeft">3dip</item><itemname="expandableListPreferredItemIndicatorRight">33dip</item><itemname="expandableListPreferredChildIndicatorLeft">?android:attr/expandableListPreferredItemIndicatorLeft</item><itemname="expandableListPreferredChildIndicatorRight">?android:attr/expandableListPreferredItemIndicatorRight</item><!--Galleryattributes--><itemname="galleryItemBackground">@android:drawable/gallery_item_background</item><!--Windowattributes--><itemname="windowBackground">@android:drawable/screen_background_dark</item><itemname="windowFrame">@null</item><itemname="windowNoTitle">false</item><itemname="windowFullscreen">false</item><itemname="windowIsFloating">false</item><itemname="windowContentOverlay">@android:drawable/title_bar_shadow</item><itemname="windowShowWallpaper">false</item><itemname="windowTitleStyle">@android:style/WindowTitle</item><itemname="windowTitleSize">25dip</item><itemname="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item><itemname="android:windowAnimationStyle">@android:style/Animation.Activity</item><itemname="android:windowSoftInputMode">stateUnspecified|adjustUnspecified</item><!--Dialogattributes--><itemname="alertDialogStyle">@android:style/AlertDialog</item><!--Panelattributes--><itemname="panelBackground">@android:drawable/menu_background</item><itemname="panelFullBackground">@android:drawable/menu_background_fill_parent_width</item><!--Thesethreeattributesdonotseemstobeusedbytheframework.Declaredpublicthough--><itemname="panelColorBackground">#000</item><itemname="panelColorForeground">?android:attr/textColorPrimary</item><itemname="panelTextAppearance">?android:attr/textAppearance</item><!--Scrollbarattributes--><itemname="scrollbarFadeDuration">250</item><itemname="scrollbarDefaultDelayBeforeFade">300</item><itemname="scrollbarSize">10dip</item><itemname="scrollbarThumbHorizontal">@android:drawable/scrollbar_handle_horizontal</item><itemname="scrollbarThumbVertical">@android:drawable/scrollbar_handle_vertical</item><itemname="scrollbarTrackHorizontal">@null</item><itemname="scrollbarTrackVertical">@null</item><!--Textselectionhandleattributes--><itemname="textSelectHandleLeft">@android:drawable/text_select_handle_left</item><itemname="textSelectHandleRight">@android:drawable/text_select_handle_right</item><itemname="textSelectHandle">@android:drawable/text_select_handle_middle</item><itemname="textSelectHandleWindowStyle">@android:style/Widget.TextSelectHandle</item><!--Widgetstyles--><itemname="absListViewStyle">@android:style/Widget.AbsListView</item><itemname="autoCompleteTextViewStyle">@android:style/Widget.AutoCompleteTextView</item><itemname="checkboxStyle">@android:style/poundButton.CheckBox</item><itemname="dropDownListViewStyle">@android:style/Widget.ListView.DropDown</item><itemname="editTextStyle">@android:style/Widget.EditText</item><itemname="expandableListViewStyle">@android:style/Widget.ExpandableListView</item><itemname="expandableListViewWhiteStyle">@android:style/Widget.ExpandableListView.White</item><itemname="galleryStyle">@android:style/Widget.Gallery</item><itemname="gestureOverlayViewStyle">@android:style/Widget.GestureOverlayView</item><itemname="gridViewStyle">@android:style/Widget.GridView</item><itemname="imageButtonStyle">@android:style/Widget.ImageButton</item><itemname="imageWellStyle">@android:style/Widget.ImageWell</item><itemname="listViewStyle">@android:style/Widget.ListView</item><itemname="listViewWhiteStyle">@android:style/Widget.ListView.White</item><itemname="popupWindowStyle">@android:style/Widget.PopupWindow</item><itemname="progressBarStyle">@android:style/Widget.ProgressBar</item><itemname="progressBarStyleHorizontal">@android:style/Widget.ProgressBar.Horizontal</item><itemname="progressBarStyleSmall">@android:style/Widget.ProgressBar.Small</item><itemname="progressBarStyleSmallTitle">@android:style/Widget.ProgressBar.Small.Title</item><itemname="progressBarStyleLarge">@android:style/Widget.ProgressBar.Large</item><itemname="progressBarStyleInverse">@android:style/Widget.ProgressBar.Inverse</item><itemname="progressBarStyleSmallInverse">@android:style/Widget.ProgressBar.Small.Inverse</item><itemname="progressBarStyleLargeInverse">@android:style/Widget.ProgressBar.Large.Inverse</item><itemname="seekBarStyle">@android:style/Widget.SeekBar</item><itemname="ratingBarStyle">@android:style/Widget.RatingBar</item><itemname="ratingBarStyleIndicator">@android:style/Widget.RatingBar.Indicator</item><itemname="ratingBarStyleSmall">@android:style/Widget.RatingBar.Small</item><itemname="radioButtonStyle">@android:style/poundButton.RadioButton</item><itemname="scrollViewStyle">@android:style/Widget.ScrollView</item><itemname="horizontalScrollViewStyle">@android:style/Widget.HorizontalScrollView</item><itemname="spinnerStyle">@android:style/Widget.Spinner</item><itemname="starStyle">@android:style/poundButton.Star</item><itemname="tabWidgetStyle">@android:style/Widget.TabWidget</item><itemname="textViewStyle">@android:style/Widget.TextView</item><itemname="webTextViewStyle">@android:style/Widget.WebTextView</item><itemname="webViewStyle">@android:style/Widget.WebView</item><itemname="dropDownItemStyle">@android:style/Widget.DropDownItem</item><itemname="spinnerDropDownItemStyle">@android:style/Widget.DropDownItem.Spinner</item><itemname="spinnerItemStyle">@android:style/Widget.TextView.SpinnerItem</item><itemname="dropDownHintAppearance">@android:style/TextAppearance.Widget.DropDownHint</item><itemname="keyboardViewStyle">@android:style/Widget.KeyboardView</item><itemname="quickContactBadgeStyleWindowSmall">@android:style/Widget.QuickContactBadge.WindowSmall</item><itemname="quickContactBadgeStyleWindowMedium">@android:style/Widget.QuickContactBadge.WindowMedium</item><itemname="quickContactBadgeStyleWindowLarge">@android:style/Widget.QuickContactBadge.WindowLarge</item><itemname="quickContactBadgeStyleSmallWindowSmall">@android:style/Widget.QuickContactBadgeSmall.WindowSmall</item><itemname="quickContactBadgeStyleSmallWindowMedium">@android:style/Widget.QuickContactBadgeSmall.WindowMedium</item><itemname="quickContactBadgeStyleSmallWindowLarge">@android:style/Widget.QuickContactBadgeSmall.WindowLarge</item><!--Preferencestyles--><itemname="preferenceScreenStyle">@android:style/Preference.PreferenceScreen</item><itemname="preferenceCategoryStyle">@android:style/Preference.Category</item><itemname="preferenceStyle">@android:style/Preference</item><itemname="preferenceInformationStyle">@android:style/Preference.Information</item><itemname="checkBoxPreferenceStyle">@android:style/Preference.CheckBoxPreference</item><itemname="yesNoPreferenceStyle">@android:style/Preference.DialogPreference.YesNoPreference</item><itemname="dialogPreferenceStyle">@android:style/Preference.DialogPreference</item><itemname="editTextPreferenceStyle">@android:style/Preference.DialogPreference.EditTextPreference</item><itemname="ringtonePreferenceStyle">@android:style/Preference.RingtonePreference</item><itemname="preferenceLayoutChild">@android:layout/preference_child</item><!--Searchwidgetstyles--><itemname="searchWidgetCorpusItemBackground">@android:color/search_widget_corpus_item_background</item></style>

我们可以看到里面定义了关于我们整个应用中文字的样式,按钮的样式,列表的样式,画廊的样式,窗体的样式,对话框的样式等。这个样式是系统的默认样式,也是最符合HOLO的样式。Theme中定义的是最基本的主题样式,Theme的样式扩展样式有我们上面的Theme.Light还有Theme.NoTitleBar、Theme.NoTitleBar.Fullscreen、Theme.Light.NoTitleBar、Theme.Light.NoTitleBar.Fullscreen、Theme.Black、......

三、自定义主题

有了上面对Theme的了解之后,下面我们通过改变标题栏来自定义主题样式,首先继承Theme,标题栏是与窗体样式(Window attributes)相关的样式,我们在Theme.xml中找到这几句代码.

<!--Windowattributes--><itemname="windowBackground">@android:drawable/screen_background_dark</item><itemname="windowFrame">@null</item><itemname="windowNoTitle">false</item><itemname="windowFullscreen">false</item><itemname="windowIsFloating">false</item><itemname="windowContentOverlay">@android:drawable/title_bar_shadow</item><itemname="windowShowWallpaper">false</item><itemname="windowTitleStyle">@android:style/WindowTitle</item><itemname="windowTitleSize">25dip</item><itemname="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item><itemname="android:windowAnimationStyle">@android:style/Animation.Activity</item><itemname="android:windowSoftInputMode">stateUnspecified|adjustUnspecified</item>

将上面主题中Title的大小和背景覆盖

<!--自定义的主题样式--><stylename="myTheme"parent="android:Theme"><itemname="android:windowTitleBackgroundStyle">@style/myThemeStyle</item><itemname="android:windowTitleSize">50dip</item></style><!--主题中Title的背景样式--><stylename="myThemeStyle"><itemname="android:background">#FF0000</item></style>

默认主题样式

自定义主题样式

如果有的朋友还想改变整个应用的字体、或者风格都可以通过继承Theme覆盖原有的样式来达到自定义的效果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。