style樣式和them主題

效果如下:

style:爲什麼要引入,例如我們在做佈局時,有很多控件大小、寬度、顏色都一樣,我們複製起來很爽,但是一要修改就特別痛苦,引入style後,要修改十分方便。並且style還可以被其他新style繼承重寫,做部分修改也是相當快捷

如何設置syle:(以一個文本爲例)

1、在value資源文件中找到style.xml

2、設置好所需的屬性(注意只能手打,千萬不要複製,否則報錯,吐血!)

 <!-- 自定義一個style 方便直接統一修改-->
    <style name="MyStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#000000</item>
    </style>

一般繼承和點繼承

  <!--MyStyle2 繼承了MyStyle 可以對其中屬性進行修改-->
    <style name="MyStyle2" parent="@style/MyStyle">
        <item name="android:textSize">30sp</item>
    </style>

    <!--MyStyle3 繼承了MyStyle 可以對其中屬性進行修改  這種方式更常用-->
    <style name="MyStyle.MyStyle3">
        <item name="android:textColor">#FF4081</item>
    </style>

佈局中引用style

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   <TextView
       android:id="@+id/textView"
       android:text="Hello world Style1"
       style="@style/MyStyle" />

   <TextView
       android:text="Hello world Style2"
       style="@style/MyStyle2" />

   <TextView
       android:text="Hello world Style3"
       style="@style/MyStyle.MyStyle3" />
</LinearLayout>

them(清單文件使用) 和style 區別:使用位置不一樣,定義方式一樣

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章