loại bỏ bóng bên dưới tiện ích AppBarLayout android


94

Khi sử dụng AppBarLayoutwidget trong thư viện hỗ trợ thiết kế, một bóng đổ sẽ xuất hiện ở cuối thanh công cụ. Làm cách nào để loại bỏ cái bóng đó?

Câu trả lời:


232

Đơn giản chỉ cần sử dụng app:elevation="0dp"bên trong "AppBarLayout" để loại bỏ bóng. Nó luôn luôn làm việc cho tôi. Hi vọng nó sẽ giúp ích cho bạn.


62
Không sử dụng android: elevation. Sử dụng ứng dụng: độ cao.
náo nhiệt

3
Có cách nào để thực hiện việc này theo chương trình mà không nhận được cảnh báo rằng cài đặt độ cao chỉ khả dụng sau L không?
davidcv5

2
app: elevation = "0dp", bóng râm bị loại bỏ, nhưng bây giờ các tab không thể nhấp được.
Sandeep P

8
Đặt nó thành 0dp là ẩn Thanh công cụ.
Shajeel Afzal

1
Thật không may không còn là một câu trả lời làm việc. Xem câu trả lời của Liu Teng bên dưới vớisetOutlineProvider
Matthew

49

Sự cố này chỉ xảy ra khi phiên bản api> = 21, nếu bạn không muốn thay đổi độ cao, bạn có thể sử dụng:

appBar.setOutlineProvider(null);

nhớ kiểm tra phiên bản api


BIÊN TẬP :

Blow là mã nguồn của setOutlineProvider.

   /**
     * Sets the {@link ViewOutlineProvider} of the view, which generates the Outline that defines
     * the shape of the shadow it casts, and enables outline clipping.
     * <p>
     * The default ViewOutlineProvider, {@link ViewOutlineProvider#BACKGROUND}, queries the Outline
     * from the View's background drawable, via {@link Drawable#getOutline(Outline)}. Changing the
     * outline provider with this method allows this behavior to be overridden.
     * <p>
     * If the ViewOutlineProvider is null, if querying it for an outline returns false,
     * or if the produced Outline is {@link Outline#isEmpty()}, shadows will not be cast.
     * <p>
     * Only outlines that return true from {@link Outline#canClip()} may be used for clipping.
     *
     * @see #setClipToOutline(boolean)
     * @see #getClipToOutline()
     * @see #getOutlineProvider()
     */
    public void setOutlineProvider(ViewOutlineProvider provider) {
        mOutlineProvider = provider;
        invalidateOutline();
    }

Người ta nói rằng If the ViewOutlineProvider is null, if querying it for an outline returns false, or if the produced Outline is {@link Outline#isEmpty()}, shadows will not be cast.

Vì vậy, nếu bạn muốn loại bỏ bóng, bạn nên sử dụng phương pháp này thay vì thiết lập app:elevation. Có vẻ như việc thay đổi độ cao để loại bỏ bóng là một loại tác dụng phụ. Và việc thay đổi độ cao có thể gây ra một số vấn đề khác trong một số trường hợp.


API chỉ có sẵn từ phiên bản 21.
chakrapani

Làm gì với API <21?
DYS

vấn đề này chỉ xảy ra khi api> = 21
Liu Teng

9

Đối với tất cả những người không muốn sử dụng bringToFront()elevation="0dp"làm cho thanh công cụ biến mất:

app:elevation="0dp"kết hợp với android:translationZ="0.1dp"làm việc cho tôi.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp"
    android:translationZ="0.1dp"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@null"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

8

Với các phiên bản appcompat mới nhất, cài đặt thủ thuật app:elevation="0.1dp"trong xml không hoạt động nữa.

Cho đến nay tôi đã tìm ra hai giải pháp.

  1. Thay vì cài đặt app:elevation, hãy thử sử dụng stateListAnimator. Ví dụ, trong mã:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        StateListAnimator stateListAnimator = new StateListAnimator();
        stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(appBarLayout, "elevation", 0.1f));
        appBarLayout.setStateListAnimator(stateListAnimator);
    }
    
  2. Một cách dễ dàng hơn là bạn vẫn đặt app:elevation="0dp"trong xml như bình thường, nhưng trong mã:

    appBarLayout.bringToFront();

Tín dụng dành cho hai cuộc thảo luận sau:

ToolBar biến mất khi thiết lập độ cao cho AppBarLayout

khi đặt ứng dụng: elevation = "0dp" thì hamburgermenu không hiển thị trên thanh công cụ


3

Sử dụng android:stateListAnimator="@null". Không có tác dụng phụ.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:stateListAnimator="@null"
    >

2

Tôi đã thử app:elevation="0dp"nhưng thanh công cụ không xuất hiện nữa, nhưng việc sử dụng app:elevation="0.1dp"đã thực hiện thủ thuật.

Hy vọng điều này sẽ giúp ai đó khác.


0.1dp cũng không hoạt động, Nó cũng đang ẩn Menu Thanh công cụ.
Shajeel Afzal

Tôi có một ứng dụng đang hoạt động với appcompat v23.0.1 bằng cách sử dụng mẹo này, bạn có phiên bản nào?
Gueorgui Obregon

Tôi đang sử dụng v25.0.0.
Shajeel Afzal

2
Có vẻ như nó không hoạt động nữa với các phiên bản cập nhật :(.
Gueorgui Obregon

2

Thêm ứng dụng: elevation = "0dp" trên AppBarLayout của bạn. như ví dụ này

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

Câu trả lời cứu mạng :)
user2672052

1

Về mặt lập trình, bạn có thể sử dụng cái này: getSupportActionBar (). SetElevation (0.0f);


0

Đây là cách tôi nghĩ ra app:elevation="0dp"để loại bỏ bóng đổ.

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.