灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2015回复:0

Android主题与样式

楼主#
更多 发布于:2012-09-06 13:51


一、样式
样式是属性的集合,例如定义属性fontColor、fontSize、layout_width、layout_height等,以独立的资源文件存放在XML文件中,并设置样式的名称。
Android Style类似网页设计中的级联样式CSS设计思路,可以让设计与内容分离,并且可以方便的继承、覆盖、重用。


1.未使用Style
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:orientation="vertical"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    >
    
<TextView
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:textColor="#00FF00"
    Android:text="@string/hello" />
    
<TextView
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:text="@string/hello" />

<Button  
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:text="@string/hello"
/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:orientation="vertical"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    >
  
<TextView
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:textColor="#00FF00"
    Android:text="@string/hello" />
  
<TextView
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:text="@string/hello" />
<Button
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="@string/hello"
/>
</LinearLayout>上述界面中有三个控件,2个TextView、1个Button。他们的布局宽度layout_width与布局高度layout_height都是wrap_content包裹内容。
下面看看,如何使用Style来改进。


2、使用Style
首先,在res/values/下创建Style XML资源文件,这里创建的Style资源文件名命名为styles.xml,这个可以自己自定义。
styles.xml内容如下:
[html] <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="wrap_content">
        <item name="Android:layout_width">wrap_content</item>
        <item name="Android:layout_height">wrap_content</item>
    </style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
  <item name="Android:layout_width">wrap_content</item>
  <item name="Android:layout_height">wrap_content</item>
</style>
</resources>其中,style标签中name属性类似CSS中的class name,item标签中的name对应属性的名字,item标签对中的text对应属性的值。


使用样式:
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:orientation="vertical"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    >
    
<TextView
    style="@style/wrap_content"
    Android:textColor="#00FF00"
    Android:text="@string/hello" />
    
<TextView
    style="@style/wrap_content"
    Android:text="@string/hello" />

<Button  
    style="@style/wrap_content"
    Android:text="@string/hello"
/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:orientation="vertical"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    >
  
<TextView
    style="@style/wrap_content"
    Android:textColor="#00FF00"
    Android:text="@string/hello" />
  
<TextView
    style="@style/wrap_content"
    Android:text="@string/hello" />
<Button
style="@style/wrap_content"
Android:text="@string/hello"
/>
</LinearLayout>
3、样式的继承
有两种方式来实现继承,一是通过style的parent属性,二是使用类似CSS中的命名规则来实现。
一、通过parent属性
修改styles.xml
[html] <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="wrap_content">
        <item name="Android:layout_width">wrap_content</item>
        <item name="Android:layout_height">wrap_content</item>
    </style>
    
    <style name="inherit" parent="wrap_content">
        <item name="Android:textColor">#00FF00</item>
    </style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
  <item name="Android:layout_width">wrap_content</item>
  <item name="Android:layout_height">wrap_content</item>
</style>

<style name="inherit" parent="wrap_content">
  <item name="Android:textColor">#00FF00</item>
</style>
</resources>新增名为inherit的样式,并且继承名为wrap_content样式,也就是说inherit具有wrap_content样式中定义的属性参数。
方式:style="@style/inherit"


二、通过命名规则实现
[html] <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="wrap_content">
        <item name="Android:layout_width">wrap_content</item>
        <item name="Android:layout_height">wrap_content</item>
    </style>
    
    <style name="wrap_content.inherit">
        <item name="Android:textColor">#00FF00</item>
    </style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
  <item name="Android:layout_width">wrap_content</item>
  <item name="Android:layout_height">wrap_content</item>
</style>

<style name="wrap_content.inherit">
  <item name="Android:textColor">#00FF00</item>
</style>
</resources>通过“.”号实现继承。
方式:style="@style/wrap_content.inherit"

二、主题
针对应用中所有Activity或者针对某个Activity设置样式,可以通过编辑AndroidManifest.xml来完成。
1.设置应用中所有Activity活动的主题
[html] <application Android:theme="@style/wrap_content">
<application Android:theme="@style/wrap_content">这样,应用中所有Activity中的所有组件都会默认使用包裹布局。


2.设置某个指定的Activity主题
[java] <activity Android:theme="@style/wrap_content">
<activity Android:theme="@style/wrap_content">
另外,Android提供了许多自带的主题样式。例如Theme.Dialog、Theme.Translucent等等。使用方式也很简单
[html] <activity Android:theme="@Androidtyle/Theme.Dialog">
<activity Android:theme="@Androidtyle/Theme.Dialog">



喜欢0 评分0
游客

返回顶部