为什么不能使用 toolbarStyle 为主题中的工具栏设置 layout_height 和 layout_width?

问题描述

挑战:我正在尝试通过默认工具栏主题为工具栏设置 layout_height 和 layout_width,如下所示:

在我的主题中:

<item name="toolbarStyle">@style/Toolbar</item>

和工具栏样式:

<style name="Toolbar" parent="Widget.AppCompat.Toolbar">
    <item name="android:background">@color/colorBackground</item>
    <item name="android:titleTextColor">@color/colorTextToolbar</item>
    <item name="android:layout_height">?attr/actionBarSize</item>
    <item name="android:layout_width">match_parent</item>
</style>

问题:layout_heightlayout_width 没有从我的主题中删除,当我从工具栏中删除 android:layout_heightandroid:layout_width 时,工具栏完全消失并显示错误缺少这些属性。尽管 android:backgroundandroid:titleTextColor 实际上是从我的主题中删除的。

我尝试过的:Here 我读到我必须在顶部添加 <resources xmlns:android="http://schemas.android.com/apk/res/android">。这实际上没有任何影响,并且顶部已经有 <resources xmlns:tools="http://schemas.android.com/tools">

你知道为什么这不起作用吗?这可能是因为 inflater 如何从样式的优先级中获​​取属性?

更新:似乎toolbarStyle 不允许设置工具栏的高度和宽度。我想这可能是在 android 文档中的某个地方定义的,尽管我无法找到确切的位置。当我点击工具栏对象时,我发现

public Toolbar(@NonNull Context context,@Nullable AttributeSet attrs) {
    this(context,attrs,R.attr.toolbarStyle);
}

作为构造器,所以它需要 R.attr.toolbarStyleR.attr.toolbarStyle 作为参数,当我查看 toolbarStyle 属性时,我只能看到
<attr format="reference" name="toolbarStyle"/>,这并没有让我知道“toolbarStyle”真正设置了什么。有没有办法找出来?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)