问题描述
我有一个活动,该活动的顶部有4个片段,底部有4个项目的BottomNavigationView.在移动设备上运行正常.当我使用LANDASCAPE平板电脑时,我想将BottomNavigationView沿垂直方向移动到活动的左侧,如下所示.
I have an activity that have 4 fragment on top and BottomNavigationView with 4 items on bottom. It is working fine in mobile devices. When i go for tablet with LANDASCAPE i want to move that BottomNavigationView to left of the activity with vertical orientation like below.
这是使用BottomNavigationView可以实现的,还是应该转到NavigationMenu android.
Is this achievable using BottomNavigationView or should I go for NavigationMenu android.
推荐答案
好吧,我通过调整方向更改和移动了一些视图来完成了BottomNavigationView所需的操作.首先,为了改变方向,我将其添加到了AndroidManifest的Activity标签中
Well i managed to do it, what you want with BottomNavigationView, by some tweaking in orientation change and moving some views.Firstly for tap into orientation change i added this to Activity tag in AndroidManifest
android:configChanges="orientation|screenSize"
在我的活动中,我添加了
and in my Activity i added following
@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>
当然,您必须根据手机的需要更改navigation.getLayoutParams().width,setX和setY.您可以创建一个函数来计算位置.抱歉,它是在白天的10:30,并且整夜都在编码,所以要休息一下,所以无法编写该函数.但是,如果您愿意,我会编写该函数.这是我为手机Vivo Y51L工作的结果,您可以查看屏幕截图.
Definitely, you have to change navigation.getLayoutParams().width, setX and setY according to your phone needs. You can create a function to calculate the position. Sorry its 10:30 in the day and was coding all night so going for rest, so couldn't write that function. But i would write that function if you want. Its what i got working for my phone Vivo Y51L, you can have look at screenshots.
屏幕截图
这篇关于如何创建垂直的BottomNavigationView android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!