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 đó?
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:
Đơ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.
setOutlineProvider
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.
Đối với tất cả những người không muốn sử dụng bringToFront()và 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>
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.
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);
}
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ụ
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.
v25.0.0.
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>
Về mặt lập trình, bạn có thể sử dụng cái này: getSupportActionBar (). SetElevation (0.0f);