Android横屏模式下如何设置底部导航栏标签全屏显示?
嘿,我来帮你搞定横屏时底部导航栏标签占满全屏宽度的需求!下面给你两种实用的解决方案,你可以根据自己的偏好来选:
方案1:利用布局资源限定符(推荐,更简洁)
这种方式不需要写Java/Kotlin代码,纯XML就能搞定。我们可以给横屏模式单独创建一套布局,专门调整底部导航栏的样式。
- 首先在你的项目
res目录下创建layout-land文件夹(如果还没有的话),这个文件夹里的布局会在屏幕横屏时被系统自动加载。 - 在
layout-land里创建activity_main.xml,内容和原布局基本一致,只需要修改BottomNavigationView的属性:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:id="@+id/Conslayout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 保留你原有的其他布局内容 --> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" <!-- 关键设置:让每个item平分宽度 --> app:itemWidth="0dp" app:itemHorizontalPadding="0dp" app:itemIconGravity="top"/> </android.support.constraint.ConstraintLayout>
这里的app:itemWidth="0dp"加上默认的权重机制,会让每个导航标签自动平分整个屏幕宽度,itemHorizontalPadding="0dp"则移除了item之间的多余间距,确保完全撑满。
方案2:代码动态调整(更灵活,适合需要额外逻辑的场景)
如果需要在横屏切换时做更多自定义操作,比如调整文字大小、图标位置,就可以用代码动态修改。
- 先在
AndroidManifest.xml里给你的MainActivity加上配置,避免屏幕旋转时重建Activity:
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize"> <!-- 其他原有配置 --> </activity>
- 然后在MainActivity里重写
onConfigurationChanged方法,根据屏幕方向调整底部导航栏:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); BottomNavigationView bottomNav = findViewById(R.id.bottom_nav); BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNav.getChildAt(0); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 横屏模式:设置每个item平分宽度 for (int i = 0; i < menuView.getChildCount(); i++) { BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) itemView.getLayoutParams(); params.width = 0; params.weight = 1; // 让每个item权重相同,平分宽度 itemView.setLayoutParams(params); // 移除左右内边距,消除空隙 itemView.setPadding(0, itemView.getPaddingTop(), 0, itemView.getPaddingBottom()); } } else { // 竖屏模式:恢复默认样式 for (int i = 0; i < menuView.getChildCount(); i++) { BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) itemView.getLayoutParams(); params.width = LinearLayout.LayoutParams.WRAP_CONTENT; params.weight = 0; itemView.setLayoutParams(params); // 恢复系统默认的水平内边距 int padding = getResources().getDimensionPixelSize(R.dimen.design_bottom_navigation_item_padding_horizontal); itemView.setPaddingRelative(padding, itemView.getPaddingTop(), padding, itemView.getPaddingBottom()); } } }
两种方案都能实现你要的效果,方案1更省心,方案2适合需要动态调整其他属性的场景,你可以按需选择~
内容的提问来源于stack exchange,提问作者Nikson




