Hãy FrameLayout
này vẽ bóng và this gradient drawable xml:
public class DrawShadowFrameLayout extends FrameLayout {
private Drawable mShadowDrawable;
private final int mShadowElevation = 8;
private int mWidth;
private int mHeight;
private boolean mShadowVisible = true;
public DrawShadowFrameLayout(Context context) {
this(context, null, 0);
init();
}
public DrawShadowFrameLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init();
}
public DrawShadowFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mShadowDrawable = ContextCompat.getDrawable(getContext(), R.drawable.shadow);
if (mShadowDrawable != null) {
mShadowDrawable.setCallback(this);
}
setWillNotDraw(!mShadowVisible);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
updateShadowBounds();
}
private void updateShadowBounds() {
if (mShadowDrawable != null) {
mShadowDrawable.setBounds(0, 0, mWidth, mShadowElevation);
}
ViewCompat.postInvalidateOnAnimation(this);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (mShadowDrawable != null && mShadowVisible) {
getBackground().setBounds(0, mShadowDrawable.getBounds().bottom, mWidth, mHeight);
mShadowDrawable.draw(canvas);
}
}
public void setShadowVisible(boolean shadowVisible) {
setWillNotDraw(!mShadowVisible);
updateShadowBounds();
}
int getShadowElevation() {
return mShadowVisible ? mShadowElevation : 0;
}
}
Quấn bạn BottomNavigationView
bên này bố trí này như:
<DrawShadowFrameLayout>
<BottomNavigationView />
</DrawShadowFrameLayout>
Thật không may, bóng mẹ đẻ được vẽ dưới xem, chúng ta phải bắt chước này lên bóng mình.
Đừng quên thêm android:elevation="8dp"
cho số DrawShadowFrameLayout
nữa.
Another approach is extendingBottomNavigationView
và ghi đè draw()
để thực hiện tương tự. Điều này sẽ giúp bạn mất một FrameLayout
trong hệ thống phân cấp chế độ xem của mình.
Nguồn
2016-11-01 09:59:24
Đúng là bóng chỉ xuất hiện khi nền là màu trắng. Thật là ngu xuẩn. Bất kỳ ý tưởng tại sao? – WindRider
Tôi đồng ý nó rất thất vọng và không có một đầu mối tại sao. – luksha
Hi @ luksha Tôi làm chính xác những gì bạn nói, nhưng tôi vẫn không làm việc: (đáng ngạc nhiên, nếu tôi đặt BottomNavigationBar trên đầu trang của màn hình, Nó nâng cao biên giới dưới cùng của giao diện điều hướng. Tuy nhiên, khi tôi đặt nó ở dưới cùng của màn hình, độ cao dừng hoạt động –