我想創建底部導航欄顯示菜單項圖標和除中心項外的文本。例如。我已附加圖像 如何使底部導航顯示除中心項目菜單以外的圖標和文本的菜單項只顯示圖標?
4
A
回答
11
嘗試以下代碼:
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@color/gray"
android:foregroundGravity="top"
android:gravity="top">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomnav"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_weight="1"
android:background="null"
app:itemIconTint="@color/green"
app:itemTextColor="@color/green"
app:menu="@menu/main">
</android.support.design.widget.BottomNavigationView>
<ImageView
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_weight="0.5"
android:src="@drawable/camera" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomnav1"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_weight="1"
android:background="null"
app:itemIconTint="@color/green"
app:itemTextColor="@color/green"
app:menu="@menu/main">
</android.support.design.widget.BottomNavigationView>
</LinearLayout>
</LinearLayout>
Helper類:
public class BottomNavigationViewHelper {
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
在活動:
BottomNavigationView bnav = (BottomNavigationView) findViewById(R.id.bottomnav);
BottomNavigationViewHelper.disableShiftMode(bnav);
菜單文件:
<item
android:id="@+id/place"
android:icon="@drawable/ic_place_black_24dp"
android:title="Place"/>
<item
android:id="@+id/home"
android:icon="@drawable/ic_account_balance_black_24dp"
android:title="Home"/>
+0
感謝您的回覆,但我已完成此代碼。我想要像在底部導航項目中的第三個圖標相機。 –
+0
你可以嘗試水平線性佈局。並且您可以在兩者之間添加兩個底部導航視圖和一個圖像視圖。 –
+0
看到我編輯的答案 –
相關問題
- 1. 底部導航菜單圖標項
- 2. 該圖標不顯示在菜單項
- 3. 圖標菜單項不顯示
- 4. android菜單項不顯示圖標?
- 5. 顯示菜單和菜單項目
- 6. 操作欄不顯示菜單項,既圖標和文本
- 7. 如何在菜單圖標右側顯示菜單圖標?
- 8. 文本菜單圖標不顯示
- 9. Android:選項菜單外觀和圖標未顯示
- 10. 如何在操作欄的多個菜單項中顯示圖標和文本?
- 11. 底部導航視圖菜單項標題大小
- 12. 沒有在Android選項菜單項中顯示的圖標
- 13. 如何在點擊時隱藏和顯示菜單項圖標?
- 14. 單擊菜單項時顯示導航下拉菜單
- 15. 如何在菜單項後顯示我的導航菜單中的文本視圖?
- 16. 顯示底部菜單
- 17. 使用視圖顯示節點的菜單父項的標題
- 18. android - 如何在底部導航視圖中設置菜單項?
- 19. 只顯示子菜單項與他的父菜單項
- 20. 如何在Android中單擊更多項目後顯示菜單圖標
- 21. 在上下文菜單標題中顯示選定的項目
- 22. 選項菜單中未顯示文本
- 23. Android底部導航菜單4項
- 24. 如何在android中的上下文菜單中顯示圖標?
- 25. xamarin充氣菜單操作欄圖標不顯示,只顯示文本
- 26. WPF文本菜單綁定菜單項的圖標到菜單項頭
- 27. Bootstrap - 對齊導航欄的品牌徽標和選項菜單(圖標+文本)
- 28. ActionbarSherlock沒有顯示菜單圖標
- 29. Android Studio:沒有菜單圖標顯示
- 30. Python Tkinter - 菜單圖標不顯示
看到我的編輯答案。 –
你不能期望人們爲你寫代碼。你試過什麼了? –