我成功地实现了你想要的关于BottomNavigationView的功能,通过在方向改变时进行一些调整和移动一些视图。首先,为了在方向改变时进行操作,我在AndroidManifest中的Activity标签中添加了以下内容:
android:configChanges="orientation|screenSize"
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
BottomNavigationView navigation= (BottomNavigationView) findViewById(R.id.navigation);
navigation.setRotation(90f);
navigation.getLayoutParams().width=480;
navigation.requestLayout();
navigation.setY(600f);
navigation.setX(-435f);
// navigation.requestLayout();
BottomNavigationMenuView menuView = (BottomNavigationMenuView) navigation.getChildAt(0);
for (int i = 0; i < menuView.getChildCount(); i++) {
final View iconView = menuView.getChildAt(i);
iconView.setRotation(-90f);
}
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
recreate();
}
}
我的XML文件是:
<?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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="@string/title_home"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
由于不能自定义BottomNavigation以垂直方式使用,建议(如果您仍未能解决此问题)切换到此库:SideMenu-Android 或者您可以创建一个自定义的导航抽屉。
<item
android:id="@+id/text"
android:title="@string/about"
android:icon="@android:drawable/ic_menu_info_details"
app:showAsAction="never"/>
app:showAsAction="never"
放在每个菜单项中并尝试...你不能将BottomNavigationView移动到布局的左侧区域。
BottomNavigationView不符合平板电脑的材料设计规范
唯一的方法是在启动时检查屏幕方向,例如:
if (islandcape()){
//Create the Navigation view layout here..
}else{
//Create the BottomNavigationView layout here ..
}
DrawerLayout
中自定义NavigationView
以获得这样的UI。 - Jeel Vankhede