`
runfeel
  • 浏览: 904758 次
文章分类
社区版块
存档分类
最新评论

Style和Theme

 
阅读更多

样式资源

Style是一个包含一种或者多种格式化属性的集合,我们可以将其用为一个单位用在布局XML单个元素当中。比如,我们可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。
Theme
:是一个包含一种或者多种格式化属性的集合,我们可以将其为一个单位用在应用中所有的Activity当中或者应用中的某个Activity当中。比如,我们可以定义一个Theme,它为window framepanel 的前景和背景定义了一组颜色,并为菜单定义可文字的大小和颜色属性,可以将这个Theme应用在你程序当中所有的Activity里。

样式资源定义了用户界面(UI)的格式和外观。样式能被应用到单独的View (通过置入layout 文件),或者整个Activity及应用程序(通过置入manifest文件)。

styletheme都是资源,android提供了很多这样的默认资源。你可以来使用它们。同时你也可以自己定义styletheme。这非常的简单,只需要在res/values/这个路径里面新建一个.xml文件,而且他的根节点必须是<resources>.对每一个styletheme,给<style>element增加一个全局唯一的名字,也可以选择增加一个父类属性,我们写的styletheme就会继承这个父类的属性。styletheme的定义格式相同。不过style是针对view来说的,比如TextViewEditText这些,而theme必须针对整个activity或者整个程序,你必须在AndroidManifest.xml中的<application>或者<activity>中定义。

注意:样式是简单类型资源,是用名称(name)属性(而非XML文件名)来直接引用的。因此,在一个XML文件里,可以把样式资源和其他简单类型资源一起放入一个<resources>元素下。

控件的Style设计就范围大多了,看看Eclipse的Android控件属性编辑器[Properties]就大概知道有哪些条目,而Android内置的style.xml也只是定义每个控件的默认样式而已....不过控件的style不建议大改,耐看的style更能让用户长时间使用软件。另外,控件的Style在很多情况下都用到9.png,学习9.png就必须到\base\core\res\res\drawable-hdpi里面看看,里面有很多系统内置的9.png。

Theme是针对窗体级别的,改变窗体样式;

Style是针对窗体元素级别的,改变指定控件或者Layout的样式。


文件

res/values/filename.xml

文件名可随意指定。元素的名称name将被用作资源ID。


资源引用

XML代码: @[package:]style/style_name


语法

<?xmlversion="1.0" encoding="utf-8"?>

<resources>

<style name="style_name" parent="@[package:]style/style_to_inherit">

<itemname="[package:]style_property_name"> style_value </item>

</style>

</resources>



元素

<resources>

必填项。必须是根元素。

无属性。

<style>

定义单个样式。包含<item>元素。

属性:

name

String类型。必填项。样式的名称,作为资源ID应用到View、Activity或应用程序。

parent

Style资源。本样式的父资源,将继承其Style属性。

<item>

为样式定义单个属性。必须是<style> 元素的子元素。

属性:

name

属性资源必填项。指定样式属性的名称,必要的话带上包(package)前缀(比如android:textColor)。


以下属性是在Themes中比较常见的,源自Android系统本身的themes.xml:

<!-- Window attributes -->
<item name="windowBackground">@android:drawable/screen_background_dark</item>
<item name="windowFrame">@null</item>
<item name="windowNoTitle">false</item>
<item name="windowFullscreen">false</item>
<item name="windowIsFloating">false</item>
<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>
<item name="windowTitleStyle">@android:style/WindowTitle</item>
<item name="windowTitleSize">25dip</item>
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Activity</item>

Theme:

就像style一样,主题依然在<style>元素里边申明,也是以同样的方式引用。

不同的是你通过在Android Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个 Activity,但是主题是不能应用在某一个单独的View里。

主题的一个例子:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<stylename="CustomTheme">
<itemname="android:windowNoTitle">true</item>
<itemname="windowFrame">@drawable/screen_frame</item>
<itemname="windowBackground">@drawable/screen_background_white</item>
<itemname="panelForegroundColor">#FF000000</item>
<itemname="panelBackgroundColor">#FFFFFFFF</item>
<itemname="panelTextColor">?panelForegroundColor</item>
<itemname="panelTextSize">14</item>
<itemname="menuItemTextColor">?panelTextColor</item>
<itemname="menuItemTextSize">?panelTextSize</item>
</style>
</resources>

注意:我们用了@符号和?符号来应用资源。

@符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。

问号?表明了我们引用的资源的值在当前的主题当中定义过。通过引用在<item>里边定义的名字可以做到(panelTextColor用的颜色和panelForegroundColor中定义的一样)。这中技巧只能用在XML资源当中。

设置使用主题(theme):

1.在manifest当中设置主题

1.为了在成用当中所有的Activity当中使用主题,你可以打开AndroidManifest.xml 文件,编辑<application>标签,让其包含android:theme属性,值是一个主题的名字,如下:

<applicationandroid:theme="@style/CustomTheme">

2.如果你只是想让你程序当中的某个Activity拥有这个主题,那么你可以编辑<activity>标签

Android中提供了几种内置的资源,有好几种主题你可以切换而不用自己写。比如你可以用对话框主题来让你的Activity看起来像一个对话框。在manifest中定义如下:<activity android:theme="@android:style/Theme.Dialog">(看前面链接有很多theme)

补充:

如果你喜欢一个主题,但是想做一些轻微的改变,你只需要将这个主题添加为父主题。比如我们修改Theme.Dialog主题。我们来继承Theme.Dialog来生成一个新的主题。

<stylename="CustomDialogTheme" parent="@android:style/Theme.Dialog">

继承了Theme.Dialog后,我们可以按照我们的要求来调整主题。我们可以修改在Theme.Dialog中定义的每个item元素的值,然后我们在Android Manifest 文件中使用CustomDialogTheme 而不是Theme.Dialog。

2.在程序当中设置主题

如果需要的话,你可 以在Activity当中通过使用方法setTheme()来加载一个主题。注意,如果你这么做的话,你应该初始化任何View之前设置主题。比如,在调 用setContentView(View) 和inflate(int, ViewGroup)方法前。这保证系统将当前主题应用在所有的UI界面。例子如下:

protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.linear_layout_3);
}

如果你打算在程序代码中来加载主界面的主题,那么需要注意主题当中不能包括任何系统启动这个Activity所使用的动画,这些动画将在程序启动前显示。在很多情况下,如果你想将主题应用到你的主界面,在XML中定义似乎是一个更好的办法。

Android提供的style样式

Android平台定义的主题样式:

android:theme="@android:style/Theme.Dialog"<wbr><wbr></wbr></wbr>将一个Activity显示为对话框模式
•android:theme="@android:style/Theme.NoTitleBar"<wbr></wbr>
不显示应用程序标题栏
•android:theme="@android:style/Theme.NoTitleBar.Fullscreen"<wbr></wbr>
不显示应用程序标题栏,并全屏
•android:theme="@android:style/Theme.Light"<wbr></wbr>
背景为白色
•android:theme="@android:style/Theme.Light.NoTitleBar"<wbr></wbr>
白色背景并无标题栏
•android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"<wbr></wbr>
白色背景,无标题栏,全屏
•android:theme="@android:style/Theme.Black"<wbr></wbr>
背景黑色
•android:theme="@android:style/Theme.Black.NoTitleBar"<wbr></wbr>
黑色背景并无标题栏
•android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"<wbr><wbr><wbr></wbr></wbr></wbr>
黑色背景,无标题栏,全屏
•android:theme="@android:style/Theme.Wallpaper"<wbr></wbr>
用系统桌面为应用程序背景
•android:theme="@android:style/Theme.Wallpaper.NoTitleBar"<wbr></wbr>
用系统桌面为应用程序背景,且无标题栏
•android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"<wbr></wbr>
用系统桌面为应用程序背景,无标题栏,全屏
•android:theme="@android:style/Translucent"
半透明效果
•android:theme="@android:style/Theme.Translucent.NoTitleBar"<wbr></wbr>
半透明并无标题栏
•android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"<wbr></wbr>
半透明效果,无标题栏,全屏
•android:theme="@android:style/Theme.Panel"

Android平台定义了三种字体大小:

"?android:attr/textAppearanceLarge"
"?android:attr/textAppearanceMedium"
"?android:attr/textAppearanceSmall"

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/textColorSecondaryInvers<wbr>e"</wbr>

AndroidProgressBar样式:

style="?android:attr/progressBarStyleHorizont<wbr>al"<br> style="?android:attr/progressBarStyleLarge"<br> style="?android:attr/progressBarStyleSmall"<br> style="?android:attr/progressBarStyleSmallTit<wbr>le"</wbr></wbr>

分隔符

横向: <View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
纵向: <Viewandroid:layout_width="1dip"
android:layout_height="fill_parent"
android:background="?android:attr/listDivider" /

CheckBox样式<wbr></wbr><wbr></wbr>

style="?android:attr/starStyle"


类似标题栏效果的TextView
style="?android:attr/listSeparatorTextViewSty<wbr>le"<br><br></wbr>
其它有用的样式
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingRight="?android:attr/scrollbarSize"
style="?android:attr/windowTitleBackgroundSty<wbr>le"<br> style="?android:attr/windowTitleStyle"<br> android:layout_height="?android:attr/windowTitleSize"<br> android:background="?android:attr/windowBackground"</wbr>

<wbr></wbr>

修改Activity的标题栏样式

如在styles.xml中增加
<
resources>
<wbr><wbr>&lt;stylename="AutoWindowTitleBackgroun<wbr>d"&gt;<br><wbr><wbr><wbr><wbr>&lt;item name="android:background"&gt;#778899&lt;/item&gt;<br><wbr><wbr>&lt;/style&gt;<br><wbr><wbr>&lt;stylename="autoWindowTitlebar"parent="android:Theme"&gt;<br><wbr><wbr><wbr><wbr>&lt;item name="android:windowTitleSize"&gt;32dp&lt;/item&gt;<br><wbr><wbr><wbr><wbr>&lt;itemname="android:windowTitleBackgroundSty<wbr>le"&gt;@style/AutoWindowTitleBackgroun<wbr>d&lt;/item&gt;<br><wbr><wbr>&lt;/style&gt;<br> &lt;/resources&gt;<br><span style="color:#494949">接着再修改</span><span style="color:#494949">AndroidManifest.xml</span><span style="color:#494949">文件,找到要自定义标题栏的</span><span style="color:#494949">Activity</span><span style="color:#494949">,添加上</span><span style="color:#494949">android:theme</span><span style="color:#494949">值,比如:</span><span style="color:#494949"><br></span><code><span style="color:#494949">&lt;</span>activity</code><span style="color:#494949"></span><code><span style="color:#494949">android:name</span>=".MainActivity"</code><span style="color:#494949"></span><code><span style="color:#494949">android:theme</span>="@style/autoWindowTitlebar"&gt;</code></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

去掉所有Activity界面的标题栏
修改AndroidManifest.xml

application 标签中添加android:theme=”@android:style/Theme.NoTitleBar”

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics