android - 如何根据自己的喜好控制材质工具栏的高度?
问题描述
这是我的布局文件,如您所见,我没有使用主题附带的 AppBar,而是使用自定义工具栏。
代码:
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/dice_roller_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/activity_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:title="@string/app_name" />
</com.google.android.material.appbar.AppBarLayout>
<ImageView.../>
<Button.../>
</androidx.constraintlayout.widget.ConstraintLayout>
这就是它的样子:https ://imgur.com/cPSFads
(注意这里没有elevation
使用属性)。
我正在从Theme.MaterialComponents.Light.NoActionBar
主题扩展以能够使用我自己的自定义ToolBar
. 现在的问题是,我找不到改变工具栏高度的方法。为了改变海拔,我尝试使用android:elevation
<AppBarLayout> 标签内的属性,如下所示:
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
...
android:elevation="4dp"
...>
<com.google.android.material.appbar.MaterialToolbar.../>
</com.google.android.material.appbar.AppBarLayout>
正如您在此处看到的那样,它没有任何区别 (我已经尝试了从 0dp 到 4dp 的所有值,它没有任何区别)
然后我在android:elevation
<AppBarLayout> 的子标签内添加了相同的属性,即 <MaterialToolbar> 标签,如下所示:
<com.google.android.material.appbar.AppBarLayout...>
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/activity_toolbar"
...
android:elevation="4dp"
... />
</com.google.android.material.appbar.AppBarLayout>
这导致了像这样的可怕混乱, 这绝对不是我想要的。
然后我尝试app:elevation
了在 <com.google.android.material.appbar.MaterialToolbar/> 标签中使用时没有做任何更改的属性,无论我使用的值如何,但是当我app:elevation
在 <com.google.android 中尝试相同的属性时.material.appbar.AppBarLayout>,它去掉了原始截图中可以看到的一点点高程(显示了没有elevation
使用属性时应用程序的状态),并使它看起来像这样(注意没有高程不再在顶部栏中)。
我不只是想增加更多的高度或降低高度,我希望能够控制它。我知道已经有足够的海拔来遵守材料指南,但我想让它像没有海拔的谷歌新闻应用程序一样,或者我可能想让它像股票 android 中的设置页面一样,那里有足够的海拔使它看起来既漂亮又与布局的其余部分区分开来。
如果需要更多信息,您可以查看我制作的此应用的 github 存储库:https ://github.com/sbeve72/diceroller/tree/unguided-development
PS:我是初学者,你可能已经注意到这只是你在谷歌上 Udacity 课程时制作的第一个应用程序。
解决方案
尝试删除AppBarLayout
并设置android:background
您的MaterialToolbar
,届时可以控制海拔android:elevation
:
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="0dp" />
查看最新的 Material Design Library 稳定版 1.4
推荐阅读
- python - 处理每个类方法调用
- java - 如何解决 Java IndexOutOfBoundsException?
- javascript - 如何从 Javascript 的类构造函数中访问分配给对象的变量名?
- php - 将变量保存到 PHP 中的 .txt 文件?
- c++ - C++ 读取数据文件
- r - ggplot2 geom_rect 在 facet_grid 下不能适当缩放
- android - 滑动图像加载神秘地失败
- javascript - 由于 Visual Studio MVC 中的 div,CSS 未加载
- javascript - Mongoose - 返回引用文档数组时如何选择特定字段?
- kubernetes - 删除 node-role.kubernetes.io/master:NoSchedule 污点