本帖最後由 tonyh 於 2018-7-21 17:40 編輯
試著於 styles.xml 新增風格,並於配置元件時直接套用風格設定。在此以 TextView 為例。
layout/activity_main.xml- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
- android:orientation="vertical">
- <TextView
- android:text="@string/hello"
- style="@style/Style01"
- android:id="@+id/tv1"
- android:layout_height="50sp"
- android:layout_width="match_parent"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="50sp"
- android:text="@string/hello"
- android:id="@+id/tv2"
- android:layout_gravity="center_horizontal"
- style="@style/Style02" />
- </LinearLayout>
複製代碼 values/styles.xml- <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="Style01">
- <item name="android:background">#e20000</item>
- <item name="android:textColor">#fff0f0</item>
- <item name="android:textSize">30sp</item>
- <item name="android:gravity">center</item>
- </style>
- <style name="Style02" parent="Style01">
- <item name="android:background">#006de2</item>
- <item name="android:textColor">#f0f6ff</item>
- <item name="android:layout_marginTop">10dp</item>
- </style>
- </resources>
複製代碼 |