Android style样式和theme主题


style样式:


style样式可以指定控件的宽,高,字体大小,颜色等等。
类似于css样式。可以使设计与内容分离。

创建样式,在value文件夹下的style.xml文件:

<resources>
<!--resources根标签-->
    <!-- 系统自带的样式。parent继承自 Theme.AppCompat.Light.DarkActionBar-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>
    <!-- 自己定义的style样式,设置了宽高,字体颜色,字体大小-->
    <style name="textstyle_one">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#999999</item>
        <item name="android:textSize">25sp</item>
    </style>
    <!-- 自己定义的style样式,继承自textstyle_one,style_one中的样式textstyle_two都可使用,
    由于textstyle_two中也用了textColor。重新定义的将覆盖掉原有的。
    -->
    <style name="textstyle_two" parent="textstyle_one">
        <item name="android:textColor">#A52A2A</item>
    </style>
</resources>


在activity_main.xml文件下调用:
用style标签调用即可;

<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
        style="@style/textstyle_one"
        android:text="@string/hello_world"
        />
    <TextView
        style="@style/textstyle_two"
        android:text="hello易烊千玺"
    />
</LinearLayout>

效果图:

在这里插入图片描述


theme主题:


theme主题是应用到整个activity和application的样式,而不单单是应用到某个视图。
设置好主题后整个activity的视图都将采用这个主题。
当主题和样式中的属性发生冲突时,样式的优先级高于主题。
主题和样式的代码结构是一样的,不同的是在引用方式上。

在values目录下打开style.xml文件编写theme主题:
编写theme主题,继承了parent="Theme.AppCompat.Light.DarkActionBar"来保证兼容性,否则会出现异常
定义了一个灰色的背景主题

<resources>
<!--resources根标签-->
    <!-- 系统自带的样式。parent继承自 Theme.AppCompat.Light.DarkActionBar-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>
    <!-- 自己定义的style样式,设置了宽高,字体颜色,字体大小-->
    <style name="textstyle_one">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#999999</item>
        <item name="android:textSize">25sp</item>
    </style>
    <!-- 自己定义的style样式,继承自textstyle_one,style_one中的样式textstyle_two都可使用,
    由于textstyle_two中也用了textColor。重新定义的将覆盖掉原有的。
    -->
    <style name="textstyle_two" parent="textstyle_one">
        <item name="android:textColor">#A52A2A</item>
    </style>


    <!-- 编写theme主题,,继承了parent="Theme.AppCompat.Light.DarkActionBar"来保证兼容性,否则会出现异常-->
    <style name="graytheme" parent="Theme.AppCompat.Light.DarkActionBar">

        <item name="android:background">#999999</item>
    </style>
</resources>

在AndroidManifest.xml文件下来引用这个主题。

也可以在activity.java文件下来引用,在oncreate()方法里添加setTheme()即可;
例如:
setTheme(R.style.graytheme);
在activity标签中添加android:theme="@style/graytheme"属性
AndroidManifest.xml文件的位置:
在这里插入图片描述
打开进行编辑

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.syx.styleandtheme" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!-- 引用设定好的主题-->
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/graytheme"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

在这里插入图片描述

发布了46 篇原创文章 · 获赞 0 · 访问量 506

猜你喜欢

转载自blog.csdn.net/qq_45844648/article/details/105026639