Android app与Activity主题配置

一、样式和主题(style and theme)

  1.1 样式(style)是来指定视图和窗口的外观和格式的一组属性集合。样式可以指定文本、字体及大小、背景颜色等属性。比如:

 1 <resources>
 2 
 3     <style name="customfont">
 4         <item name="android:layout_width">match_parent</item>
 5         <item name="android:layout_height">wrap_content</item>
 6         <item name="android:textColor">#d05050</item>
 7         <item name="android:textSize">16sp</item>
 8         <item name="android:gravity">center</item>
 9     </style>
10 
11 </resources>

 

  样式在TextView控件中使用:

1 <TextView
2         android:id="@+id/show_activity_id"
3         style="@style/customfont"
4         android:text="hello"/>

  1.2 样式继承

  style可以通过paren属性继承一个现在的样式,然后,修改或者添加属性。比如:

 1 <resources>
 2     
 3     <style name="customfont">
 4         <item name="android:layout_width">match_parent</item>
 5         <item name="android:layout_height">wrap_content</item>
 6         <item name="android:textSize">16sp</item>
 7         <item name="android:gravity">center</item>
 8     </style>
 9     
10     <style name="customfontcolor" parent="customfont">
11         <item name="android:textColor">#d05050</item>
12     </style>
13 
14 </resources>

 

  当前,在继承自已定义样式时,也可以不需要parent属性,只需要在新的样式名称前,加上将要继承样式的名称,名称间用“.”分隔。比如:

1 <resources>
2 
3     <style name="customfont.customfontcolor">
4         <item name="android:textColor">#d05050</item>
5     </style>
6 
7 </resources>

  1.2 为Activity 或者 Application 设置主题

  为Activity或者Application设置主题,在Androidmanfest.xml文件中,在<Activity>(或者<Application>)标签中,设置Android:theme属性。比如:

 1 <application
 2     android:allowBackup="true"
 3     android:icon="@mipmap/ic_launcher"
 4     android:label="@string/app_name"
 5     android:supportsRtl="true"
 6     android:theme="@style/AppTheme">
 7 
 8     <activity
 9         android:name=".Activity.SecondActivity"
10         android:launchMode="singleTop"
11         android:theme="@style/customttheme"/>
12 
13 </application>

 

  假如,继承的系统主题并不符合要求,也可以继承这个主题,修改或者添加属性,如下:

<resources>

    <style name="customttheme" parent="@style/AppTheme">
        <item name="android:colorBackground">#666</item>
    </style>

</resources>

 

 

 

posted @ 2017-05-07 15:06  naray  阅读(4974)  评论(0编辑  收藏  举报