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