UI基礎style樣式&Theme

來源:互聯網
上載者:User

利用style可以為layout中任何對象以xml方式定義外觀,例如給文設定textSize,textColor等,

 

建立一個xml檔案,任意命名例如style.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources>

<style name="DavidStyleText1">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#EC9237</item>
</style>

<style name="DavidStyleText2">
<item name="android:textSize">14sp</item>
<item name="android:textColor">#FF7F7C</item>
<item name="android:fromAlpha">0.0</item>
<item name="android:toAlpha">0.0</item>
</style>

</resources>

每個style通過name屬性區別或調用,例如在main.xml中為兩個textview分別添加這兩個樣式:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 套用樣式1的TextView -->
<TextView

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/str_text_view1"
/>
<!-- 套用樣式2的TextView -->
<TextView

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/str_text_view2"
/>
</LinearLayout>

另外<style>元素中有一個parent屬性。這個屬性可以讓當前樣式繼承一個父樣式,並且具有父樣式的值。當然,如果父樣式的值不符合你的需求,你也可以對它進行修改,如下: 

<?xml version="1.0" encoding="utf-8"?> 
<resources>
<style name="subitcast" parent="@style/parentstyle">
<item name="android:textColor">#FF0000</item>
</style>
</resources>

關於theme,他的定義形式上與style一樣,兩者的區別在與應用地方不同,theme針對整個應用或Activity

主題對整個應用或某個Activity進行全域性影響。如果一個應用使用了主題,同時應用下的view也使用了樣式,那麼當主題和樣式屬性發生衝突時,樣式的優先順序高於主題。 
另外android系統也定義了一些主題,例如:<activity android:theme=“@android:style/Theme.Dialog”>,該主題可以讓Activity看起來像一個對話方塊,還有透明主題:@android:style/Theme.Translucent 。如果需要查閱這些主題,可以在文檔的referenceandroid-->R.style 中查看。 

<?xml version="1.0" encoding="utf-8"?> 
<resources>
<style name=“itcastTheme">
<item name=“android:windowNoTitle”>true</item> <!– 沒標題
<item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!– 全螢幕顯示
</style>
</resources>

上面“?android:windowNoTitle”中的問號用於引用在當前主題中定義過的資源的值。下面代碼顯示在AndroidManifest.xml中如何為應用設定上面定義的主題: 
<application android:icon="@drawable/icon" android:label="@string/app_name" 
     android:theme="@style/myTheme"> 
   ...... 
</application> 
除了可以在AndroidManifest.xml中設定主題,同樣也可以在代碼中設定主題,如下: 
setTheme(R.style.myTheme); 
(結合傳智資料整理)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.