2015-01-27 41 views
7

Tôi đang sử dụng thanh công cụ từ AppCompat V7 để thay thế thanh hành động trước đó và muốn có bóng của thanh công cụ như thanh hành động trước đó. nhưng thanh công cụ không có bóng theo mặc định và tôi đã thử các bản sửa lỗi được đề cập từ reddit. nhưng không may mắn.Material Design - Thanh công cụ AppCompat không hiển thị bóng

mã để thiết lập cái bóng:

mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 

Cách bố trí thanh công cụ:

<?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"> 

bố trí hoạt động:

<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" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@+id/user_icon" 
        android:gravity="center" 
        android:layout_gravity="center_vertical" 
        android:layout_centerVertical="true" 
        android:textSize="14sp" 
        android:text="@string/not_logged_in" 
        android:paddingTop="0dp" 
        android:paddingBottom="0dp"/> 
     </RelativeLayout> 
     <ListView 
      android:id="@+id/drawer_list" 
      android:layout_below="@+id/drawer_header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:divider="#eee" 
      android:background="#fff" 
      android:dividerHeight="0dp" /> 
    </RelativeLayout> 

</android.support.v4.widget.DrawerLayout> 

Setting trong style.xml:

<style name="myAppTheme" parent="Theme.AppCompat.Light"> 
    <item name="colorPrimary">@color/primaryColor</item> 
    <item name="colorPrimaryDark">@color/primaryColorDark</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="windowActionBar">false</item> 
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
    <item name="android:windowContentOverlay">@drawable/drawer_shadow</item> 
</style> 

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> 
    <item name="spinBars">true</item> 
    <item name="color">@android:color/black</item> 
</style> 

bất cứ ai có thể giúp đỡ?

cảm ơn!

cập nhật 1: với đề xuất từ ​​Willis, tôi nhận được bóng được hiển thị, nhưng nó không phải là bên dưới thanh công cụ, thay vào đó là bên trái của thanh công cụ. enter image description here

Cập nhật 2: Tôi đã nhận thấy rằng nếu tôi không đặt cửa sổContentOverlay trong toolbar.xml và styles.xml, bóng sẽ thực sự ở trên cùng của thanh công cụ. enter image description here

+0

Hai là bóng tối hoàn toàn khác nhau. Cái dọc là của 'DrawerLayout'. Nó được cho là được hiển thị bên cạnh ngăn kéo mở rộng. Phần nằm ngang là một phần của 'windowContentOverlay' trên các API bên dưới' LOLLIPOP'. Khi bạn làm việc với tiện ích 'Thanh công cụ', thanh công cụ không phải là một phần của trang trí cửa sổ để bóng bắt đầu ở đầu cửa sổ trên thanh công cụ thay vì bên dưới thanh công cụ. Bạn cần thêm một 'View' trống bên dưới' Toolbar' pre-'LOLLIPOP' với nền của nó được đặt thành một bóng dọc có thể vẽ được (cao 8dp '# 20000000' đến' # 00000000'). Trên 'LOLLIPOP' bạn có thể đặt độ cao 8dp trên thanh công cụ. –

+0

Hi Eugen, bạn đã cứu tôi trong ngày. Đây chính xác là những gì tôi cần, và nó hoạt động rất tốt dưới Lollipop trước. điều này sẽ là anwser chính xác. Bạn có thể đăng nó như là câu trả lời và tôi có thể chấp nhận điều này không. cảm ơn rất nhiều! –

+0

Chỉ cần làm điều đó, vui vì tôi có thể phục vụ. –

Trả lời

4

Hai thứ đó hoàn toàn khác nhau. Hình minh hoạ dọc là một trong số DrawerLayout. Nó được cho là được hiển thị bên cạnh ngăn kéo mở rộng. ngang một là một phần của windowContentOverlay trên API bên dưới LOLLIPOP (trên LOLLIPOP@null).

Khi bạn làm việc với Toolbar phụ tùng thanh công cụ không phải là một phần của cửa sổ trang trí nữa vì vậy cái bóng bắt đầu ở phía trên cùng của cửa sổ qua thanh công cụ thay vì dưới đây nó (vì vậy bạn muốn windowContentOverlay@null). Ngoài ra, bạn cần thêm một số trống rỗng View bên dưới thanh công cụ trước LOLLIPOP với nền được đặt thành độ bóng có thể kéo dọc (độ cao 8dp từ #20000000 đến #00000000 hoạt động tốt nhất). Trên LOLLIPOP, bạn có thể đặt độ cao 8dp trên thanh công cụ thay thế.

Lưu ý: Sử dụng cùng một độ dốc nhưng ngang như bóng ngăn để có kết quả tốt nhất.

2

Bạn có thể đặt lượng bóng bằng cách sử dụng phương pháp setElevation. Ví dụ:

getSupportActionBar().setElevation(25); 

Tăng/giảm giá trị được chuyển đến setElevation do đó sẽ tăng/giảm sự hiện diện của hiệu ứng đổ bóng.

+0

cảm ơn Willis, có một số nhầm lẫn trong câu hỏi của tôi, thực sự tôi cần phải hiển thị bóng tối, nhưng tôi không tìm thấy giải pháp để làm cho nó hoạt động. có thể đặt giá trị khác cho độ cao để hiển thị bóng không? –

+0

Xin lỗi tôi đã đọc sai câu hỏi. Tôi đã chỉnh sửa câu trả lời của mình. – Willis

+0

Xin chào Willis, cảm ơn rất nhiều! Tôi cũng đặt chủ đề cho ứng dụng. Tôi đã nhận được bóng hiển thị bây giờ, nhưng nó không phải là dưới thanh công cụ. bất kỳ đề nghị nào khác? –

0

Để hiển thị bóng dưới thanh công cụ, vui lòng sử dụng AppBarLayout có sẵn trong Thư viện hỗ trợ thiết kế của Google Android.Đây là một ví dụ về cách nó nên được sử dụng.

<android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    <android.support.v7.widget.Toolbar 
      android:layout_height="?attr/actionBarSize" 
      android:layout_width="match_parent"/> 
    </android.support.design.widget.AppBarLayout> 

Để sử dụng thiết kế Thư viện hỗ trợ Google Android nhập sau vào tập tin build.gradle của bạn:

compile 'com.android.support:design:22.2.0' 
Các vấn đề liên quan