我正在使用支持库v21中的新工具栏更新我的应用程序.我的问题是,如果我没有设置"高程"属性,工具栏不会投射任何阴影.这是正常行为还是我做错了什么?
我的代码是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
.
.
.
Run Code Online (Sandbox Code Playgroud)
在我的Activity - OnCreate方法中:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud) android material-design android-5.0-lollipop android-toolbar
我想在自定义布局元素中使用高程,我想使用默认操作栏高程值在XML中设置该元素的值.我找不到用XML获取它的方法,我想避免调用getElevation()代码.有小费吗?
这是一个简单的问题:
我使用新的支持库,它允许我拥有一个工具栏实例,甚至将其设置为活动的actionBar(或使用默认的).
如何自定义工具栏(或默认ActionBar)投射的阴影?
我试过使用"setElevation"(两者都有),但它似乎没有在Android Lollipop上做任何事情.
另外,我找不到如何在Lollipop前版本上自定义阴影.可能有这个API吗?或至少是一个drawable(我没有找到,即使我已经尝试过)?
好的,我已经设法找到了下一件事:
对于Lollipop和pre-Lollipop,当您的活动使用正常的AppCompat主题(例如"Theme.AppCompat.Light")时,阴影应该看起来很好.
但是,当您使用"setSupportActionBar"(并使用适当的主题,例如"Theme.AppCompat.Light.NoActionBar")时,您将遇到阴影的一些问题.
对于pre-Lollipop,你可以在工具栏下面放置一个FrameLayout,它看起来就像用于普通主题的那样,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:foreground="?android:windowContentOverlay"
android:visibility="@integer/action_bar_shadow_background_visibility" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:text="@string/hello_world" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
您可以使用FrameLayout作为活动的内容,或使用我所做的.您还可以通过为棒棒糖前置整数0和为棒棒糖及以上版本放置2来设置其可见性,就像我使用"action_bar_shadow_background_visibility"一样.
这是一个丑陋的解决方案,但它可以工作,但仅限于前Lollipop版本.
在任何情况下,如果我使用"setSupportActionBar",我仍然无法找到一种方法来为Lollipop显示阴影.我尝试在工具栏和操作栏上使用"setElevation"(在调用"setSupportActionBar"之后立即使用"getSupportActionBar").
我也没有得到如何获得动作栏的标准高度,因为我注意到许多教程说使用"minHeight"作为工具栏.非常感谢这方面的帮助.
谁能帮我这个?
再说一次,为了说清楚,这就是我的要求:
编辑:好的,到目前为止,这是模仿Lollipop的影子:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
layout="@layout/toolbar_action_bar_shadow" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:text="@string/hello_world" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
RES /布局/ …
android android-actionbar android-support-library android-elevation
android ×3