取消选择并规范化默认菜单项选择
Deselect and Normalize default menu item selection
正如您在下图中看到的,我很难弄清楚如何取消选择第一个菜单项,因为它在 BottomNavigationView
.
上默认处于活动状态。
我已经尝试了所有方法,但第一个菜单仍然出现。
Java代码.
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomNavigation);
bottomNavigationView.setBackground(null);
bottomNavigationView.getMenu().setGroupCheckable(0, false, true);
bottomNavigationView.setElevation(0);
//bottomNavigationView.setSelectedItemId(R.id.menu2);
}
}
主要XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/main_bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp"
app:fabCradleVerticalOffset="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:menu="@menu/main_bottom_menu"
/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:backgroundTint="@color/_Surface"
app:tint="@color/_OnSurface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/dashboard_24"
app:layout_anchor="@+id/main_bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
如果您对如何规范化 BottomNavigationView
上的菜单项有任何想法,我将不胜感激。
谢谢。
public static void setCheckable(BottomNavigationView view, boolean checkable) {
final Menu menu = view.getMenu();
for(int i = 0; i < menu.size(); i++) { // Your Menu 0
menu.getItem(i).setCheckable(checkable);
}
}
由于相关菜单为0,代码可以按0排列。
尝试将此添加到 main.xml
中的 bottomNavigationView
app:labelVisibilityMode="unlabeled"
所以你的观点会是这样的:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:menu="@menu/main_bottom_menu"
app:labelVisibilityMode="unlabeled"
/>
您可以在此处查看有关您可以在视图中进行哪些更改的更多详细信息:
https://material.io/components/bottom-navigation/android#using-bottom-navigation
正如您在下图中看到的,我很难弄清楚如何取消选择第一个菜单项,因为它在 BottomNavigationView
.
我已经尝试了所有方法,但第一个菜单仍然出现。
Java代码.
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomNavigation);
bottomNavigationView.setBackground(null);
bottomNavigationView.getMenu().setGroupCheckable(0, false, true);
bottomNavigationView.setElevation(0);
//bottomNavigationView.setSelectedItemId(R.id.menu2);
}
}
主要XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/main_bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp"
app:fabCradleVerticalOffset="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:menu="@menu/main_bottom_menu"
/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:backgroundTint="@color/_Surface"
app:tint="@color/_OnSurface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/dashboard_24"
app:layout_anchor="@+id/main_bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
如果您对如何规范化 BottomNavigationView
上的菜单项有任何想法,我将不胜感激。
谢谢。
public static void setCheckable(BottomNavigationView view, boolean checkable) {
final Menu menu = view.getMenu();
for(int i = 0; i < menu.size(); i++) { // Your Menu 0
menu.getItem(i).setCheckable(checkable);
}
}
由于相关菜单为0,代码可以按0排列。
尝试将此添加到 main.xml
中的 bottomNavigationViewapp:labelVisibilityMode="unlabeled"
所以你的观点会是这样的:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:menu="@menu/main_bottom_menu"
app:labelVisibilityMode="unlabeled"
/>
您可以在此处查看有关您可以在视图中进行哪些更改的更多详细信息: https://material.io/components/bottom-navigation/android#using-bottom-navigation