2015-11-04 110 views

回答

0

添加工具栏或appbar或者低于这个观点你要显示的影子。

<View 
     android:id="@+id/shadow_layout" 

     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:background="@drawable/toolbar_dropshadow" /> 

------- ------ toolbar_dropshadow.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <gradient 
     android:startColor="@android:color/transparent" 
     android:endColor="#88333333" 
     android:angle="90"/> 
</shape> 
0

设置android:elevationToolbar如果你的目标是Android 5.0及以上或预棒棒糖做以下。

Toolbar.xml

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/toolbar_height" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

    <View 
     android:id="@+id/shadow" 
     android:layout_width="match_parent" 
     android:layout_height="4dp" 
     android:background="@drawable/shadow" /> 

</LinearLayout> 

shadow.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="@color/no_color" 
     android:endColor="@color/shadow_normal" 
     android:angle="90" /> 
</shape> 

color.xml

<color name="shadow_normal">#55000000</color> 
<color name="no_color">#00000000</color> 
+0

可以请你告诉我如何添加这种观点编程 – Vennila

+0

笏是你的实际需求?你必须以编程方式添加,为什么? –

+0

我有一个扩展工具栏,在这里我要补充这个视图自定义视图? – Vennila

0

,你可以简单地使用财产海拔的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:elevation="8dp" 
    android:minHeight="?attr/actionBarSize" 
    android:background="@color/colorPrimary" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
</android.support.v7.widget.Toolbar> 

如果(Build.VERSION.SDK_INT < 21),使用带有阴影背景视图的FrameLayout里面的工具栏下面

给出

如下面

<include 
    android:id="@+id/my_action_bar" 
    layout="@layout/toolbar" /> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- activity content here.... --> 

    <View 
     android:id="@+id/view_toolbar_shadow" 
     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:background="@drawable/toolbar_shadow" /> 
</FrameLayout> 

在活动代码

,你可以隐藏,如果个影子观点dk版本> = 21

View mToolbarShadow = findViewById(R.id.view_toolbar_shadow); 
     if (Build.VERSION.SDK_INT >= 21) { 
      mToolbarShadow.setVisibility(View.GONE); 
     }