Android 中的style的使用

style

其本质系统属性的集合

style是对于系统的某些属性的值的修改,应用(reference)类型均为style类型,style是用来设置单个view(控件)的。使用场景为界面属性设置。如下:

//style使用,注意style不需要添加"android"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.haolianluo.myapplication.MainActivity">
    
    //自定义style 用于TextView 控件
    <TextView
        style="@style/CustomerTextStyle" />

</android.support.constraint.ConstraintLayout>
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    //自定义style
    <style name="CustomerTextStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center</item>
        <item name="android:text">Customer Text</item>
        <item name="android:textColor">@android:color/holo_blue_bright</item>
        <item name="android:textSize">18sp</item>
    </style>
    
</resources>

单个设置style或许感觉麻烦,但对于整体风格保持一致的某些特定控件的属性统一设置却很适用,可以减少很多不必要的代码,且便于统一修改界面风格

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值