我在"首选项"屏幕上的支持库中使用新的"材质设计"工具栏时遇到问题.
我有一个settings.xml文件,如下所示:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/AddingItems"
android:key="pref_key_storage_settings">
<ListPreference
android:key="pref_key_new_items"
android:title="@string/LocationOfNewItems"
android:summary="@string/LocationOfNewItemsSummary"
android:entries="@array/new_items_entry"
android:entryValues="@array/new_item_entry_value"
android:defaultValue="1"/>
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
字符串在别处定义.
android android-actionbar android-actionbar-compat android-5.0-lollipop android-toolbar
首先,我知道之前已经问过这个问题,但之前没有得到回答.我希望有人能给我一个答案.
在我的应用程序中,我使用Appcompat_v7(API 21)中的工具栏.这是我的代码:
<android.support.v7.widget.Toolbar
style="@style/DarkActionbarStyle"
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="@dimen/actionbar_height" />
Run Code Online (Sandbox Code Playgroud)
这是我使用的ToolBar样式:
<style name="DarkActionbarStyle" parent="@style/Widget.AppCompat.Toolbar">
<item name="android:background">?attr/colorPrimary</item>
<item name="titleTextAppearance">@style/ActionBarTitle</item>
<item name="android:elevation">2dp</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">@style/ThemeActionBarDark</item>
</style>
<style name="ThemeActionBarDark" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="actionBarItemBackground">@drawable/btn_dark_orange</item>
<item name="selectableItemBackground">@drawable/btn_dark_orange</item>
</style>
Run Code Online (Sandbox Code Playgroud)
问题是,提升前棒棒糖不起作用.所以我的问题是:棒棒糖设备上的工具栏下是否有阴影?
android toolbar shadow android-appcompat android-5.0-lollipop
我将我的Android应用程序更新为新的材质设计,但我还想在工具栏中添加一些阴影或高程.通过图像/ 9补丁似乎有一些(hacky)方法,但我想知道它是否可以通过支持库完成.(就像CardView可以有高程)
根据这一答案的另一个问题,这是通过包装可能Toolbar在AppBarLayout,但这并不为我工作.
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
我也尝试过通过XML和代码设置高程,但这也不起作用.
任何帮助,将不胜感激!提前致谢.
更新:
由于我在其他布局中包含了我的工具栏布局,因此下面是我的主要布局之一:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/Toolbar" />
<fragment
class="OverAllField.XamarinAndroid.Fragments.Planning.PlanningFragment"
android:id="@+id/PlanningFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我从新的appcompat中添加了新的材质设计操作栏,并使用了新的工具栏小部件.我在xml的工具栏上设置了自定义背景但我的问题是没有显示操作栏的阴影.你知道怎么做吗?
工具栏代码
<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"/>
Run Code Online (Sandbox Code Playgroud)
MyTheme风格
<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>
Run Code Online (Sandbox Code Playgroud)
MyTheme.Popup风格
<style name="MyTheme.Popup" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColor">#ffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)
更新
就像@Justin鲍威尔建议我添加actionBarStyle我的主题,但仍然没有投影.
MyTheme风格(更新)
<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>
Run Code Online (Sandbox Code Playgroud) 这是一个简单的问题:
我使用新的支持库,它允许我拥有一个工具栏实例,甚至将其设置为活动的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
如您所知,Elevation不适用于Pre-Lollipop设备.因此,appcompat-v7中的默认App Bar使用"伪阴影"纹理,我喜欢称之为模拟阴影.我的问题是我需要使用自定义工具栏.当我使用自定义工具栏时,不存在"伪阴影".所以它看起来很平坦.知道如何添加阴影吗?有些人在其他论坛上说过添加一个前景为"android:windowContentOverlay"的FrameLayout,它以某种方式与ToolBar重叠.遗憾的是,我还没有找到任何办法让这项工作得以实现.出于某种原因,在我的测试中,"android:windowContentOverlay"无论如何都是隐形的.不知道我做错了什么.:/
这是我的工具栏的布局XML数据:
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
Run Code Online (Sandbox Code Playgroud)
以下是使用默认AppCompat AppBar的样子:http://imgur.com/0EiE1Vv
以下是使用自定义ToolBar的样子:http://imgur.com/GGEC6Tq
编辑:在alanv的帮助下,我想出了如何在工具栏下面制作阴影.但是,它与AppCompat中的默认设置不同.它只是一个微弱的阴影,如果我没记错的话,它与旧版本中使用的阴影资源相同.我正在努力寻找默认AppCompat栏的资源.
我正在使用AppCompat V7中的工具栏来替换上一个操作栏,并希望像上一个操作栏一样拥有工具栏的阴影.但默认情况下工具栏没有阴影,我尝试过reddit中提到的修复.但没有运气.
设置阴影的代码:
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
Run Code Online (Sandbox Code Playgroud)
工具栏布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="?attr/actionBarSize"
android:background="#F1F1F1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_margin="0dp"
foreground="?android:windowContentOverlay">
Run Code Online (Sandbox Code Playgroud)
活动布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent">
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout android:id="@+id/fragment_container"
android:layout_below="@id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
<!-- navigation drawer -->
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_gravity="start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:divider="#eee"
android:background="#EEE"
android:id="@+id/drawer_header">
<ImageView
android:id="@+id/user_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/user_icon"
android:src="@drawable/ic_action_person"
android:paddingTop="0dp"
android:paddingLeft="0dp"/>
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content" …Run Code Online (Sandbox Code Playgroud) 我想在ToolBar中添加阴影,并使用以下链接: ToolBar Shadow
但运行应用程序在LogCat中显示此错误:
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.Toolbar
Run Code Online (Sandbox Code Playgroud)
工具栏代码:
<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="200dp">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="200dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:theme="@style/MyCustomToolbarTheme"
android:background="@drawable/main_header">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
MainActivity XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar_main"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/app_bar"
android:text="ytkbhjk"/>
Run Code Online (Sandbox Code Playgroud)
MainActivity Java:
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)
请帮我
我正在使用这个答案的代码.我正在尝试在折叠时禁用工具栏的高程.我尝试将android:elevation="0dp"所有xml标签放入,但它没有成功.有什么建议吗?
android toolbar material-design android-collapsingtoolbarlayout
我不知道为什么android:elevation不起作用
我的代码:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".UI.Activity.ChatActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:elevation="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.07"
app:layout_constraintStart_toStartOf="parent"
android:gravity="center_vertical">
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8"
android:inputType="text"
android:layout_marginLeft="15dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:background="@drawable/border_edit_text_messenger"
android:textCursorDrawable="@color/colorWhite"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:src="@drawable/ic_send"
android:scaleType="centerInside"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud) 我试图在此示例后面的工具栏中添加阴影 /sf/answers/1883287171/
活动中的工具栏
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
Run Code Online (Sandbox Code Playgroud)
这是我添加工具栏的xml文件
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:paddingTop="@dimen/app_bar_top_padding"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:theme="@style/MyCustomToolBarTheme">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- **** Place Your Content Here **** -->
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@drawable/shadow" />
</FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我在logcat java.lang.ClassCastException中得到这个:android.widget.LinearLayout无法强制转换为android.support.v7.widget.Toolbar
我怎样才能摆脱这个错误?