Android學習隨筆之UI美化(一)--------- 樣式和主題

來源:互聯網
上載者:User

標籤:

  

1、樣式和主題的使用

樣式和主題是一樣的,都是一系列屬性的集合,只是應用的對象不同,樣式應用在單個控制項上,如果TextView,Button等.主題應用在功能資訊清單檔中的<application>或者<activity>標籤上以屬性的形式出現,如:

<application         theme=”XXX”/>

<activity     theme=”XXX”/>

 

來看一個資訊清單檔:

在這裡要明確兩點:

  1、主題是應用在application或者activity標籤之上的,用於設定整個應用程式或者某個Activity的整體外觀屬性.

  2、如果同時指定了主題,以activity標籤上的主題為準。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.day27_uitest"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="17"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name"             android:theme="@style/AppTheme"  >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

 2、自訂style (在這裡我的理解:這裡的style就像css<層疊樣式表>,用來規範一些外觀)

      MyStyle。xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">    <!--        Base application theme, dependent on API level. This theme is replaced        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.       -->    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">        <!--            Theme customizations available in newer API levels can go in            res/values-vXX/styles.xml, while customizations related to            backward-compatibility can go here.         -->    </style>    <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style>    <!-- 對應用程式定製一個樣式            使用parent屬性指定當前樣式的父樣式,這種方式即可以引用系統樣式,         也可以引用使用者自訂樣式       -->    <style name="MyAppTheme" parent="AppTheme">        <!-- All customizations that are NOT specific to a particular API-level can go here. -->        <item name="android:textColor">@android:color/white</item>    </style>    <!-- 對當前的介面Activity定製一個樣式,在acvitity中定義的樣式,對介面中所有的控制項都有效 -->    <style name="MyMainActivityTheme" parent="MyAppTheme">        <item name="android:textSize">10sp</item>        <item name="android:textColor">#00FF00</item>    </style>    <!-- 單獨對布局檔案中的控制項的樣式進行定製                使用父樣式做首碼產生子樣式,但這個用法只能引用使用者自訂樣式,           不能引用系統樣式.換句話來講,引用系統樣式只能使用parent屬性.                       如果同時使用自訂樣式做首碼和parent屬性,則以parent屬性指定的父樣式為準.             -->    <style name="MyMainActivityTheme.MyTextViewStyle" >         <item name="android:textSize">20sp</item>         <item name="android:textColor">#FF0000</item>    </style>    <style name="MyMainActivityTheme.MyTextViewStyle2">         <item name="android:textSize">40sp</item>        <item name="android:textColor">#0000FF</item>    </style>        <style name="style_mine">        <!-- ctrl+alt+向下的方向鍵完成整行複製 -->        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>    </style>    </resources>

上面這個檔案中:第一和第二個style是系統內建的樣式

第三個第四個是帶有parent(父樣式)的自訂樣式

第五個第六個是帶有父樣式首碼的自訂樣式 ,此時應注意:

  如果同時使用自訂樣式做首碼和parent屬性,則以parent屬性指定的父樣式為準.


第七個是不帶任何父樣式的自訂樣式。

Android學習隨筆之UI美化(一)--------- 樣式和主題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.