![](/img/trans.png)
[英]Is there a way to customize spacing between icons for android bottom navigation view?
[英]Customize Bottom Navigation View
我已經使用OpenCV開發了一個android應用程序。 用戶界面具有底部導航視圖。 當我在底部導航視圖中按下項目時,它將實時將不同的過濾器應用於JavaCameraView。
我的問題是底部導航看起來很平坦。 我想使按鈕之類的項目從其位置抬高,因此它們與用戶界面的其余部分不在同一平面上。
我知道底頁,但這對我沒有幫助。 我已經完全開發了該應用程序並完成了設計。 除底部導航視圖外,我無法使用任何其他視圖。
我可以在BottomNavigationView對象上使用任何XML屬性或任何方法來使Item從其位置抬高嗎?
導航視圖:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="start"
app:itemTextColor="@color/cardview_light_background"
android:background="?android:attr/windowBackground"
app:menu="@menu/nav" />
物品代碼:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/fiter1" android:title="Filter 1" />
<item android:id="@+id/filter2" android:title="Filter 2" />
<item android:id="@+id/filter3" android:title="Filter 3" />
</menu>
使用Customized類,我們可以在“曲線底部導航視圖”中繪制“曲線”自定義形狀。 通過使用以下XML
<android.tutorial.curvedbottombar.CurvedBottomNavigationView
android:id="@+id/customBottomBar"
android:layout_width="match_parent"
android:layout_height="62dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
對於Java類是
public class CurvedBottomNavigationView extends BottomNavigationView {
private Path mPath;
private Paint mPaint;
/** the CURVE_CIRCLE_RADIUS represent the radius of the fab button */
public final int CURVE_CIRCLE_RADIUS = 256 / 3;
// the coordinates of the first curve
public Point mFirstCurveStartPoint = new Point();
public Point mFirstCurveEndPoint = new Point();
public Point mFirstCurveControlPoint2 = new Point();
public Point mFirstCurveControlPoint1 = new Point();
//the coordinates of the second curve
@SuppressWarnings("FieldCanBeLocal")
public Point mSecondCurveStartPoint = new Point();
public Point mSecondCurveEndPoint = new Point();
public Point mSecondCurveControlPoint1 = new Point();
public Point mSecondCurveControlPoint2 = new Point();
public int mNavigationBarWidth;
public int mNavigationBarHeight;
public CurvedBottomNavigationView(Context context) {
super(context);
init();
}
public CurvedBottomNavigationView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CurvedBottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPath = new Path();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.WHITE);
setBackgroundColor(Color.TRANSPARENT);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// get width and height of navigation bar
// Navigation bar bounds (width & height)
mNavigationBarWidth = getWidth();
mNavigationBarHeight = getHeight();
// the coordinates (x,y) of the start point before curve
mFirstCurveStartPoint.set((mNavigationBarWidth / 2) - (CURVE_CIRCLE_RADIUS * 2) - (CURVE_CIRCLE_RADIUS / 3), 0);
// the coordinates (x,y) of the end point after curve
mFirstCurveEndPoint.set(mNavigationBarWidth / 2, CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4));
// same thing for the second curve
mSecondCurveStartPoint = mFirstCurveEndPoint;
mSecondCurveEndPoint.set((mNavigationBarWidth / 2) + (CURVE_CIRCLE_RADIUS * 2) + (CURVE_CIRCLE_RADIUS / 3), 0);
// the coordinates (x,y) of the 1st control point on a cubic curve
mFirstCurveControlPoint1.set(mFirstCurveStartPoint.x + CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4), mFirstCurveStartPoint.y);
// the coordinates (x,y) of the 2nd control point on a cubic curve
mFirstCurveControlPoint2.set(mFirstCurveEndPoint.x - (CURVE_CIRCLE_RADIUS * 2) + CURVE_CIRCLE_RADIUS, mFirstCurveEndPoint.y);
mSecondCurveControlPoint1.set(mSecondCurveStartPoint.x + (CURVE_CIRCLE_RADIUS * 2) - CURVE_CIRCLE_RADIUS, mSecondCurveStartPoint.y);
mSecondCurveControlPoint2.set(mSecondCurveEndPoint.x - (CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4)), mSecondCurveEndPoint.y);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPath.reset();
mPath.moveTo(0, 0);
mPath.lineTo(mFirstCurveStartPoint.x, mFirstCurveStartPoint.y);
mPath.cubicTo(mFirstCurveControlPoint1.x, mFirstCurveControlPoint1.y,
mFirstCurveControlPoint2.x, mFirstCurveControlPoint2.y,
mFirstCurveEndPoint.x, mFirstCurveEndPoint.y);
mPath.cubicTo(mSecondCurveControlPoint1.x, mSecondCurveControlPoint1.y,
mSecondCurveControlPoint2.x, mSecondCurveControlPoint2.y,
mSecondCurveEndPoint.x, mSecondCurveEndPoint.y);
mPath.lineTo(mNavigationBarWidth, 0);
mPath.lineTo(mNavigationBarWidth, mNavigationBarHeight);
mPath.lineTo(0, mNavigationBarHeight);
mPath.close();
canvas.drawPath(mPath, mPaint);
}
}
您可以從此處下載完整的源代碼
由於菜單項不支持高程,因此您可以設計圖標並提供一些高亮和陰影,如android UI指南中所述,這樣它們看起來就很少升高了。 有關如何設計菜單圖標的信息,請參閱本ororid指南。 https://developer.android.com/guide/practices/ui_guidelines/icon_design_menu
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.