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>

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM