【问题标题】:(android) Why does the style of the theme only partially apply?(android) 为什么主题的样式只部分适用?
【发布时间】:2021-01-01 17:11:07
【问题描述】:

我想知道为什么只应用了部分主题的样式。

我将基本主题设置为NoActionBar

我在Main.xml 中使用CoordinatorLayoutAppBarLayoutToolbar 制作了一个新工具栏。

我还为此工具栏定义了新主题样式并更改了stauts bar 颜色并应用了此主题。

但它不起作用。但是,当我更改 BaseTheme 中定义的状态栏颜色时。

效果很好。我很困惑。

我明确应用了我定义的theme style。但BaseTheme's status bar color 仍然适用。

当我更改Toolbar 颜色时,它应用得很好。

我不知道原因。

我的自定义工具栏是否受到“Manifest's Theme”的影响?

请告诉我为什么..

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"
            android:theme="@style/Theme.AppBarOverlay">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:titleTextColor="@color/black"
                app:titleMarginStart="30dp"
                android:paddingRight="30dp"
                android:theme="@style/Theme.PopupOverlay"/>
        </com.google.android.material.appbar.AppBarLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

主题.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/purple_500</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>

    <style name="Theme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
        <item name="colorPrimary">@color/light_green</item>

        <item name="android:statusBarColor" tools:targetApi="l">@color/light_green_dark</item> <!-- not apply -->
<!--        <item name="colorPrimaryVariant">@color/light_green_dark</item> ///////////////// not apply -->
<!--        <item name="colorPrimaryDark">@color/light_green_dark</item>    ///////////////// not apply -->
    </style>

    <style name="Theme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorPrimary">@color/light_green</item>
        <item name="colorPrimaryVariant">@color/light_green_dark</item>
        <item name="colorPrimaryDark">@color/light_green_dark</item>
    </style>
</resources>

Manifest.xml

<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=".data.DailyRecordDetailActivity"></activity>
        <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>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    RecyclerView rcv_dailyRecord;
    Toolbar toolbar;
    LinearLayoutManager layoutManager;

    DailyRecordAdapter dailyRecordAdapter;
    ArrayList<DailyRecordModel> dailRecordItems;

    final static String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rcv_dailyRecord = findViewById(R.id.rcv_dailyrecord);
        toolbar = findViewById(R.id.toolbar);
        
        setSupportActionBar(toolbar);
//        getSupportActionBar().setDisplayShowTitleEnabled(false);
//        toolbar.setTitle("목 록");

【问题讨论】:

  • 你能告诉你如何在你的行为中设置操作栏(java/kotlin)
  • 你的意思是在代码中?
  • 您从应用程序的主题中删除了操作栏。您是否已经在 java 中设置了它?
  • 我刚刚修改了文字。请看代码。
  • 我已经使用setSupportActionBar(toolbar)设置了toolbar

标签: android toolbar android-theme android-statusbar


【解决方案1】:

您在 styles.xml 中创建了自定义的 Theme.AppBarOverlay 样式。现在这种风格是无用的,除非你把它应用到某个视图/小部件上。

第一点:

将样式应用于特定视图,不会影响其他视图/小部件。

查看您的布局,看看您将此样式设置为哪个视图:它是 AppBarLayout 小部件。

AppBarLayout 区域不包括状态栏区域,这是因为状态栏是 android 系统组件,而不是活动小部件(不是您活动的一部分)。所以,当把这个样式应用到AppBarLayout时,它只能把这个样式的属性应用到这个AppBarLayout视图在你屏幕上占据的区域。

第二点:

每个视图/小部件都有某些可以影响其样式的属性;不仅有可以应用于任何视图的通用属性;但还有其他特定于某些视图的独特属性。将样式属性应用于无法使用它们的视图不会对这些视图产生影响,或者它们可能会对这些视图产生意外行为。

在您的示例中应用这两点:

您使用了android:statusBarColor 属性,该属性可以应用于系统/状态栏,并将其添加到应用于AppBarLayout 的样式中;所以这种样式只能应用于AppBarLayout 周边区域,即不是状态栏本身(第1点),而且AppBarLayout 不会使用它,因为它不会影响其布局中的任何内容,因为它不是可以影响AppBarLayout的部分属性(第2点)。

状态栏是一个系统组件,不是app/activity组件/widget,所以改变它必须在你应用的全局主题中,或者你可以通过编程来改变它。

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

【讨论】:

  • @ybybyb 是的,你是对的。它是AppBarLayout,包括它的任何内部视图......就像你的情况一样Toolbar......我的意思是它的周围框架,即它的宽度/高度,如果这不是描述性的......但请确保内部视图属性覆盖其父视图.. 即Toolbar 中的属性覆盖其父级AppBarLayout 中的相同/相似属性.. 例如,如果您在Toolbar 中使用android:background="@android:color/black",那么您将使用的整体颜色看到的是Toolbar黑色而不是样式中设置的AppBarLayout颜色
  • 非常感谢。我明白。然而,我还没有明白一件事。在您的回答中,在计算机Applying both points in your example: 中,我对point 2 不太了解(来自和也〜AppBarLayout)。你能更容易解释吗?对不起..
  • 我的意思是AppBarLayout 具有可以影响其外观和行为的属性集....如果您向AppBarLayout 添加一个属性(不属于该集的一部分),那么这个属性可以对AppBarLayout 的外观没有影响,也可以对其产生意外行为(因为此特定属性与AppBarLayout 属性集无关...在您的示例中,您将android:statusBarColor 放入AppBarLayout.. 但实际上这个属性不属于AppBarLayout属性集,而是属于状态栏(即只能影响状态栏)
  • 是的,或者如果您愿意,您可以在 java 中以编程方式更改它。请检查答案中的链接
  • 非常感谢..!新年快乐.. :)
猜你喜欢
  • 1970-01-01
  • 2021-08-18
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 2012-06-27
相关资源
最近更新 更多