Android style 的使用及使用场景

style的使用

  • 第一步 :打开文件 res -> values -> styles.xml
  • 第二步 :自定义自己的< style> < /style>
  1. styles 中是由 < resources>< /resources> 的根布局包括。
  2. 内部由< style > < item> < /item> < /style> 进行自定义属性。
  3. < style> < /style>中两个属性 name 和parent
    在这里插入图片描述
    name : 自定义样式名称,必填。
    parent:继承的父类,选填。该属性可以继承需要的控件的属性
  • 第三步:使用。
    styles.xml
  <!--自定义style-->
    <style name="CustomStyle">
        <item name="android:textColor">@color/colorPrimaryDark</item>
        <item name="android:textSize">10sp</item>
    </style>

    <style name="CustomStyle.changeSize">
        <item name="android:textSize">30sp</item>
    </style>

    <style name="CustomTextViewSub" parent="Widget.AppCompat.AutoCompleteTextView"></style>

activity_main.xml

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="无样式" />

    <TextView
        style="@style/CustomStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义样式"
         />

    <TextView
        style="@style/CustomStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义样式并自设大小为20sp"
        android:textSize="20sp"
        />

    <TextView
        style="@style/CustomStyle.changeSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="继承自定义样式并自设大小为30sp" />
    <TextView
        style="@style/ChangeSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="继承自定义样式并自设大小为30sp" />

    <TextView
        style="@style/CustomTextViewSub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="继承已有的父类属性" />
</LinearLayout>

在这里插入图片描述

  • 分析:
  1. 可以使用 . (点)的方式继承自定义Style,也可以使用parent 来继承自定义属性。
  2. 使用的优先级顺序,在其中看到style中如果定义了字体大小,layout布局文件中也定义了字体大小,首先应采用那个呢?下面会详细阐述,至:style的继承关系。

Style的继承关系

  • 样式属性采纳顺序:
    View自己的样式 > 上一层ViewGroup的属性 > 上上层的属性值… > activivty主题 > application 主题
<!-- 全局字体样式-->  
<style name="DefaultFontStyle">   
    <item name="android:textSize">18px</item>  
    <item name="android:textColor">#0000CC</item>  
</style>  
  
<!-- 全局背景色-->  
<style name="DefaultBgColor" parent="@style/DefaultFontStyle">   
    <item name="android:background">#F2F2F2</item>  
</style>  
  
<!-- 全局样式-->  
<style name="DefaultStyle" parent="@style/DefaultBgColor">   
</style>  
  
<!-- textView字体样式-->  
<style name="TextViewFontStyle">   
    <item name="android:textSize">20px</item>  
</style>  
  • application 设置的主题:
    xml代码:
<application android:theme="@style/DefaultStyle">  
  • activity设置的主题:
    xml代码:
<activity android:name=".AccountManageActivity"  
      android:theme="@style/DefaultStyle">  
  • TextView样式设置:
    layout布局的xml代码
 <TextView   
 android:layout_width="match_parent"   
 android:layout_height="wrap_content"  
 android:text="我在做什么:"  
 style="@style/TextViewFontStyle"  
 /> 

最终,TextView的字体大小为20px,颜色为 #0000CC.

style 的使用场景

  1. 减少重复: 在自己需要重复设置一些空间属性的时候,可以将其抽取出来,进行单独使用。例如:属性相同的文字大小及颜色设置。
  2. 全局设置: 比如全局设置背景属性、字体等。

猜你喜欢

转载自blog.csdn.net/weixin_37716758/article/details/84891430