(android) 为什么主题风格只部分适用?

如何解决(android) 为什么主题风格只部分适用?

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

我将基本主题设置为 NoActionBar

我在 CoordinatorLayout 中使用 AppBarLayoutToolbarMain.xml 创建了一个新工具栏。

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

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

效果很好。我好糊涂。

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

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

我不知道原因。

“我的自定义工具栏”是否受到“清单主题”的影响?

请告诉我为什么..

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>

themes.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("목 록");

解决方法

您在styles.xml 中创建了自定义的Theme.AppBarOverlay 样式。现在,除非您将其应用于某个视图/小部件,否则此样式将毫无用处。

第一点:

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

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

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

第二点:

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

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

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

状态栏是一个系统组件,而不是应用/活动组件/小部件,因此更改它必须在您应用的全局主题中,或者您可以通过编程方式更改。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?