应用程序中定义的主题与标题栏中的活动中设置的主题如何配合

时间:2018-08-14 13:54:49

标签: android themes titlebar

如果在AndroidMainifest.xml中放入android:theme =“ @ style / Theme.AppCompat.Light”,并且不对startActivity中的主题进行更改,则它将仅显示一个浅色标题栏。

enter image description here

但是,如果从AndroidMainifest.xml中删除android:theme =“ @ style / Theme.AppCompat.Light”并在活动的onCreate()中设置主题,则会显示两个标题栏。

enter image description here

应用程序中定义的主题如何与活动中有关标题栏的主题一起使用?

主题定义:

<!-- 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="DayTheme" parent="AppTheme">
    <item name="descriptionColor">@color/day_description</item>
</style>

<style name="NightTheme" parent="AppTheme">
    <item name="descriptionColor">@color/night_description</item>
</style>

在AndroidMainifest.xml中,它放入主题,即android:theme =“ @ style / Theme.AppCompat.Light”

<application
    android:name=".DemoApplication"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    tools:ignore="GoogleAppIndexingWarning">
    <activity android:name=".stream.StartActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

以ScrollView开头且没有工具栏的活动:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="3dp"
    android:focusable="true"
    android:focusableInTouchMode="true">
    <requestFocus />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:text="Select a Fragment to Launch:"
        android:textSize="16sp" />

    <RadioGroup
    ... ...

在startActivity类中,它设置主题,并将menuitem(图标)添加到工具栏

class StartActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        initTheme()  //<== **set the theme**

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_start)

        ......}

    fun initTheme() {
        var mPrefs =  PreferenceManager.getDefaultSharedPreferences(this)
        if ((AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) || mPrefs.getBoolean("NIGHT_THEME_MODE", false)) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            setTheme(R.style.NightTheme)
        } else {
            setTheme(R.style.DayTheme)
        }
        (this as AppCompatActivity).delegate.applyDayNight()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // **Inflate the menu; this adds items to the action bar if it is present.**
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

此外,似乎“ colorPrimary”等设置无效,一定是错误的。

<style name="DayTheme" parent="AppTheme">

    <item name="colorPrimary">#ff0000</item>
    <item name="colorPrimaryDark">#00ff00</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="descriptionColor">@color/day_description</item>
    <item name="commentsColor">@color/day_comments</item>
    <item name="timeColor">@color/day_time</item>

</style>

更新:

结果证明该项目有几个子模块,并且在一个模块的styles.xml之一中,它定义了与"AppTheme"相同的主题:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>

如果将Base application theme重命名为TestAppTheme,它将按预期工作。

现在的问题是,为什么模块的样式AppTheme将覆盖另一个定义并导致两个标题栏?

<!-- Base application theme. -->
<style name="TestAppTheme" 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="DayTheme" parent="TestAppTheme">
    <item name="descriptionColor">@color/day_description</item>
</style>

<style name="NightTheme" parent="TestAppTheme">
    <item name="descriptionColor">@color/night_description</item>
</style>

0 个答案:

没有答案