安卓:Appcompat 21如何添加阴影动作条阴影、动作、Appcompat

2023-09-05 05:34:41 作者:凉了时光旧了少年

我添加的新appcompat新材料设计动作条我用的是新的工具栏小工具。我设置一个自定义背景的工具栏上的XML,但我的问题是,不显示在动作条的阴影。你知道如何做到这一点?

工具栏code

 < android.support.v7.widget.Toolbar的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:ID =@ + ID / my_awesome_toolbar
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =match_parent
    机器人:=了minHeight?ATTR / actionBarSize
    机器人:背景=@可绘制/ ab_background_textured
    应用程序:主题=@风格/ MyTheme的
    应用程序:popupTheme =@风格/ MyTheme.Popup/>
 

MyTheme的风格

 <样式名称=MyTheme的父=Theme.AppCompat.Light.DarkActionBar>
    <项目名称=机器人:textColorPrimary> @色/ abc_primary_text_material_dark< /项目>
    <项目名称=actionMenuTextColor> @色/ abc_primary_text_material_dark< /项目>
    <项目名称=机器人:textColorSecondary>#ffff8800< /项目>
< /风格>
 

MyTheme.Popup风格

 <样式名称=MyTheme.Popup父=ThemeOverlay.AppCompat.Dark>
    <项目名称=机器人:文字颜色>#FFFFFF< /项目>
< /风格>
 
指上旅游官方版下载 指上旅游官方版app下载安装 v2.7 嗨客手机站

更新

像@Justin鲍威尔建议我加入 actionBarStyle 我的主题,但仍然没有阴影。

MyTheme的风格(更新)

 <样式名称=MyTheme的父=Theme.AppCompat.Light.DarkActionBar>
    <项目名称=机器人:textColorPrimary> @色/ abc_primary_text_material_dark< /项目>
    <项目名称=actionMenuTextColor> @色/ abc_primary_text_material_dark< /项目>
    <项目名称=机器人:textColorSecondary>#ffff8800< /项目>
    <项目名称=机器人:actionBarStyle> @风格/ Widget.AppCompat.Light.ActionBar.Solid.Inverse< /项目>
< /风格>
 

解决方案

我发现,在谷歌IO应用的解决方案是可以接受我自己,但没有看到博客或者发帖#1完全解释它。你可以做的是从https://github.com/google/iosched/blob/master/android/src/main/res/drawable-xxhdpi/bottom_shadow.9.png然后在你的活动布局:

 < RelativeLayout的机器人:layout_width =match_parent
                机器人:layout_height =match_parent>
    <包括机器人:ID =@ + ID /工具栏
             布局=@布局/工具栏/>

    <的FrameLayout机器人:layout_width =match_parent
                 机器人:layout_height =match_parent
                 机器人:layout_below =@ ID /工具栏
                 机器人:前景=@可绘制/ header_shadow>
    <  - !你的东西在这里 - >
    < /的FrameLayout>
< / RelativeLayout的>
 

而头阴影

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <项目名称=header_shadowTYPE =绘制> @可绘制/ bottom_shadow< /项目>
< /资源>
 

有关API的级别和LT; 21。就像https://github.com/google/iosched/blob/master/android/src/main/res/values/refs.xml和https://github.com/google/iosched/blob/master/android/src/main/res/values-v21/refs.xml.

和是精心制作的,这里是toolbar.xml

 < XML版本=1.0编码=UTF-8&GT?;
< android.support.v7.widget.Toolbar
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
        机器人:ID =@ + ID /工具栏
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:ATTR / colorPrimary后台=
        机器人:海拔=4DP
        应用程序:主题=@风格/ ToolbarTheme
        应用程序:popupTheme =@风格/ AppTheme/>
 

I add the new material design actionbar from the new appcompat and I use the new toolbar widget. I set a custom background on the toolbar on xml but my problem is that the drop shadow from the actionbar is not displayed. Do you know how to do this?

Toolbar code

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@drawable/ab_background_textured"
    app:theme="@style/MyTheme"
    app:popupTheme="@style/MyTheme.Popup"/>

MyTheme style

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item>
    <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item>
    <item name="android:textColorSecondary">#ffff8800</item>
</style>

MyTheme.Popup style

<style name="MyTheme.Popup" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:textColor">#ffffff</item>
</style>

Update

Like @Justin Powell suggested I add the actionBarStyle on my theme but still there is no drop shadow.

MyTheme style(Updated)

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item>
    <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item>
    <item name="android:textColorSecondary">#ffff8800</item>
    <item name="android:actionBarStyle">@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse</item>
</style>

解决方案

I found the solution in the Google IO app to be acceptable for myself, but saw no blog or Stackoverflow post fully explaining it. What you can do is grab their Apache 2 licensed drawer shadow asset from https://github.com/google/iosched/blob/master/android/src/main/res/drawable-xxhdpi/bottom_shadow.9.png and then in the layout of your Activity:

<RelativeLayout android:layout_width="match_parent"
                android:layout_height="match_parent">
    <include android:id="@+id/toolbar"
             layout="@layout/toolbar"/>

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:layout_below="@id/toolbar"
                 android:foreground="@drawable/header_shadow">
    <!-- YOUR STUFF HERE -->
    </FrameLayout>
</RelativeLayout>

Whereas header shadow is

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="header_shadow" type="drawable">@drawable/bottom_shadow</item>
</resources>

for API levels <21. Just like https://github.com/google/iosched/blob/master/android/src/main/res/values/refs.xml and https://github.com/google/iosched/blob/master/android/src/main/res/values-v21/refs.xml.

And to be elaborate, here is toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        app:theme="@style/ToolbarTheme"
        app:popupTheme="@style/AppTheme"/>