Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android:应用于工具栏及其标题的高程_Android - Fatal编程技术网

android:应用于工具栏及其标题的高程

android:应用于工具栏及其标题的高程,android,Android,我使用一种非常简单的样式来设置工具栏的格式,我使用android:elevation在工具栏下显示阴影,如下所示: <style name="AppToolbarTheme" parent="AppTheme"> <item name="android:background">@color/colorPrimary</item> <item name="android:titleTextColor">@androi

我使用一种非常简单的样式来设置工具栏的格式,我使用android:elevation在工具栏下显示阴影,如下所示:

<style
    name="AppToolbarTheme"
    parent="AppTheme">

    <item name="android:background">@color/colorPrimary</item>
    <item name="android:titleTextColor">@android:color/white</item>
    <item name="android:elevation">4dip</item>
</style>

@颜色/原色
@android:彩色/白色
4dip
我只是这样应用它:

<Toolbar
    android:id="@+id/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppToolbarTheme"/>

阴影显示在工具栏下方,就像我想要的一样,但它也以某种方式应用于工具栏的标题,看起来非常奇怪:

从样式中删除
4dip
,并将其放入
工具栏
xml

<Toolbar
    android:id="@+id/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppToolbarTheme"
    android:elevation="4dp"/>

我看了这篇文章,帮我找出了我的错误,我认为这和你的错误是一样的:

您正在主题中使用样式属性(小部件)。主题用于定义全局属性,例如
colorOnSurface、colorssurface、primaryColor
等。在这里,您使用的是:

<item name="android:background">@color/colorPrimary</item>
<item name="android:titleTextColor">@android:color/white</item>
<item name="android:elevation">4dip</item>
@color/colorPrimary
@android:彩色/白色
4dip
特定于工具栏的

我建议定义如下的样式(只要是Widget.whatever.Toolbar,就可以设置任何父级)


4dp
我目前正在使用appbar,因此您可以在此处应用高程:

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:theme="@style/Theme.AppBarTheme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</com.google.android.material.appbar.AppBarLayout>

<style name="Theme.AppBarTheme">
    <item name="elevation">6dp</item>
    <item name="toolbarStyle">@style/Widget.Toolbar</item>
</style>

<style name="Widget.Toolbar" parent="Widget.MaterialComponents.Toolbar.Surface">
    <item name="android:titleTextColor">@android:color/white</item>
</style>

6dp
@样式/Widget.Toolbar
@android:彩色/白色
您使用

android:theme=“@style/AppToolbarTheme”
在您的工具栏中,在这种情况下,它也会向嵌套组件添加高程,因为您定义了一个主题

使用

style=“@style/AppToolbarTheme”

相反。

我尝试了这个方法,效果很好,但我不满意,我想在style.xml中声明我的工具栏的所有属性。然后在layout文件夹中分离工具栏xml,并使用添加。
<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:theme="@style/Theme.AppBarTheme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</com.google.android.material.appbar.AppBarLayout>

<style name="Theme.AppBarTheme">
    <item name="elevation">6dp</item>
    <item name="toolbarStyle">@style/Widget.Toolbar</item>
</style>

<style name="Widget.Toolbar" parent="Widget.MaterialComponents.Toolbar.Surface">
    <item name="android:titleTextColor">@android:color/white</item>
</style>