【问题标题】:(android) Why custom toolbar uses BaseTheme?(android) 为什么自定义工具栏使用 BaseTheme?
【发布时间】:2020-12-22 19:28:28
【问题描述】:

我将 BaseTheme 的 Actionbar 设置为 NoActionBar,并开始使用 XML 制作 Toolbar。

但我有一个问题。

因为即使我设置了NoActionBar,我制作的工具栏的颜色仍然是BaseTheme的颜色。

我不知道为什么会这样。

另外,我没有设置工具栏的主题

为什么我设置BaseTheme的NoActionBar颜色还是一样?

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.writeweight">
    <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/Theme.WriteWeight">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent">
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize">
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rcv_dailyrecord"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toBottomOf="@id/appbar"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:scrollbars="vertical"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

值\theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.WriteWeight" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/black</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

我会等你的答复。

谢谢。

【问题讨论】:

    标签: android toolbar appbar


    【解决方案1】:

    主题是应用于整个应用、活动或视图层次结构的属性集合,而不仅仅是单个视图。当您应用主题时,应用程序或活动中的每个视图都会应用它支持的每个主题属性。主题还可以将样式应用于非视图元素,例如状态栏和窗口背景。

    这是一个文档引用,它描述了主题默认情况下可以继承给您应用中的所有 view 或 non-view 元素,除非您硬编码自己的主题.

    如果您注意到在manifest 中,您的&lt;activity&gt; 标记是&lt;application&gt; 标记的子标记,但活动以这种方式继承了&lt;application&gt; 中设置的主题,并一直向下级联此继承。

    这是一个优势,因为它可以保持应用元素的一致性和相应的用户体验。

    您可以查看the documentation 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 2016-01-02
      • 2016-05-17
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多