Android学习笔记主题(Theme)资源文件

安卓的主题资源文件,可以用于对Android应用的美化。

styles文件是主题资源文件。
定义一个主题资源格式如下:

<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="bgTheme" parent="@style/AppTheme">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowBackground">@drawable/bg</item>
    </style>
</resources>

引用主题资源可以在AndroidManifest资源中通过 android:theme="@style/AppTheme" 引用

<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引用

    <activity android:name=".MainActivity"
            android:theme="@style/bgTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

还可以通过Java代码引用

    setTheme(R.style.bgTheme);
    setContentView(R.layout.activity_main);

注意:如果使用Java代码应用要写在 setContentView(R.layout.activity_main) 之前

原文地址:https://www.cnblogs.com/lzpq/p/12885237.html