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 区别:使用位置不一样,定义方式一样

猜你喜欢

转载自blog.csdn.net/Mr_Leixiansheng/article/details/86493901