胖蔡说技术
随便扯扯

Android 中的style和Theme的使用

style和theme的定义是为了改变原有系统设定的默认窗体、字体、背景色、格式等风格而使用。其本质就是系统属性的集合。本篇主要介绍android中的style和theme的具体用法。

style和Theme

style和theme均是对于系统的莫些属性的值的修改,应用(reference)类型均为style类型,不同的是style是用来设置单个view(控件)的,而theme则是用于应用与某个节目的整体风格的。使用场景分别为界面属性设置和activity或application theme应用。如下:

//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">
    <TextView
        style="@style/CustomerTextStyle" />
</android.support.constraint.ConstraintLayout>



//theme使用

......
<application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/AppTheme">
       <activity android:name=".MainActivity"
           android:theme="@style/Animation.AppCompat.Dialog">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

//style格式设置
<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 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或许感觉麻烦,但对于整体风格保持一致的某些特定控件的属性统一设置却很适用,可以减少很多不必要的代码,且便于统一修改界面风格。theme则常见于修改界面的titlebar、actionbar、dialog风格等系统UI格式风格,但由于定制的非完全开放原因,并不能保证可以完全满足你的要求,对于一些轻量级的修改可供适用,适用其的最直接原因是,可以全局统一,保持整体风格统一。如下,是我在官网找的style和theme所用属性的源码,可供参考:

赞(0) 打赏
转载请附上原文出处链接:胖蔡说技术 » Android 中的style和Theme的使用
分享到: 更多 (0)

请小编喝杯咖啡~

支付宝扫一扫打赏

微信扫一扫打赏