Android开发学习笔记-自定义TextView属性模版

    如果项目中有很多个控件使用的是同一种样式,则为了方便,可以将样式设置到系统中去,这样使用的时候会方便很多。

    下面是自定义样式模版的方法。

1、在style.xml文件中添加自己要设置的样式内容

<resources>

    <!--
        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">
        <item name="android:windowNoTitle">true</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="text_content">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">55dp</item>
        <item name="android:layout_marginLeft">5dp</item>
        <item name="android:layout_marginTop">5dp</item>
        <!-- 自定义TextView样式 -->
    </style>

</resources>

2、在前端进行引用

 <TextView
      android:id="@+id/baom1"
      style="@style/text_content"
      android:gravity="center_vertical"
      android:drawableLeft="@android:drawable/star_big_on"
      android:text="baojing" />

3、说明:

     在xml中,name为引用的名称,item为属性名称以及对应的值。

原文地址:https://www.cnblogs.com/xuhongfei/p/4021303.html