styles.xml主题风格资源文件

1. 打开res目录下的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="blue_textview" >
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">25sp</item>
        <item name="android:textColor">#0000FF</item>
        <item name="android:textStyle">bold</item>
    </style>
    <style name="red_textview" >
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">40sp</item>
        <item name="android:textColor">#FF0000</item>
        <item name="android:textStyle">italic</item>
    </style>

</resources>

上述代码中, colorPrimary用于设定ActionBar的颜色;colorPrimaryDark用于设定状态栏的颜色;colorAccent用于设定EditText ,RadioButton和CheckBox等控件被选中时的颜色

2. 在res目录下的layout文件夹创建style_layout.xml文件

在这里插入图片描述
在这里插入图片描述

下述代码演示如何在XML文件中访问样式。

上代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="XML文件访问styles资源(加粗蓝色)"
        android:id="@+id/tv7"
        style="@style/blue_textview" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Java代码访问styles资源(斜体红色)"
        android:id="@+id/tv8" />
</LinearLayout>

上述代码中@style/blue_textview为 XML文件读取styles. xml 文件中名为 blue_textview的文字样式,以该样式作为TextView中的字体样式显示出来。

3. 在java目录下的com.example.myapplication包中创建Style_ActivityDemo类

在这里插入图片描述在这里插入图片描述

下述代码演示如何在 Java代码中访问样式。

上代码

package com.example.myapplication;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class Style_ActivityDemo extends AppCompatActivity {
    
    
    TextView tv8;
    public void onCreate(Bundle savedIntanceState) {
    
    
        super.onCreate(savedIntanceState);
        setContentView(R.layout.style_layout);
        tv8 = (TextView) findViewById(R.id.tv8);
        tv8.setTextAppearance(this,R.style.red_textview);
    }
}

在上述代码中,R.style. red_textview为Java代码读取styles.xml文件中名为red_textview的文字样式,以该样式作为TextView中的字体样式显示出来。

4. 在AndroidMainfest.xml文件添加Style_ActivityDemo.java的列表

在这里插入图片描述
在这里插入图片描述

5. 运行Color_ActivityDemo.java

在这里插入图片描述

运行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42768634/article/details/115051477