Style简单使用

使用Style的目的是为了统一风格,摒弃重复的代码,其实本质上Theme也是Style

1 使用场景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/empty1"
        android:gravity="center"
        android:text="您的列表空空如也"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/empty2"
        android:gravity="center"
        android:text="您的列表空空如也"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/empty3"
        android:gravity="center"
        android:text="您的列表空空如也"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

如上图所示,你会发现这三个TextView除了id不一样之外,其他的属性都一样,但是你又不得不重复写三遍,这还不是噩梦的开始,这只是在一个布局,其他的布局你是不是也有类似的代码?万一有一天Boss觉得对齐方式改为左对齐比较好,你是不是需要一个一个TextView里面修改呢?对,这就是代码不面向对象的坏处,缺乏重用性,不利于维护

2 Style闪亮登场

1 打开res->values->styles文件

2 自定义自己的Style

<style name="TextStyle" >
        <item name="android:layout_height">match_parent</item>
        <item name="android:text">您的列表空空如也</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_width">match_parent</item>
</style>

3 在布局文件中引用即可

<TextView
        android:id="@+id/empty1"
        style="@style/TextStyle" />
<TextView
        android:id="@+id/empty2"
        style="@style/TextStyle" />
<TextView
        android:id="@+id/empty3"
        style="@style/TextStyle" />

3 总结好处

1 封装统一风格的属性,比如我要求TextView统一左对齐,间隔2dp,大家都用这个风格就统一了样式
2 易于维护,比如现在你的老板让你把text改成“我好帅”,你不用每个TextView里面改了,直接去Style中修改即可,非常方便

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值