如何创建垂直的BottomNavigationView Android?

12

我有一个活动,其顶部有4个片段和底部有4个项目的BottomNavigationView。在移动设备上它工作得很好。当我使用横向的平板电脑时,我想将BottomNavigationView移动到活动的左侧,垂直方向如下所示。

使用BottomNavigationView能否实现这一目标,还是应该选择NavigationMenu android?

输入图片说明


通过使用bottomNavigationMenu,您无法实现这一点,但是您可以尝试以下方法:https://dev59.com/oF0a5IYBdhLWcg3w6sVk#29809691 - Abdul Aziz
您可以在DrawerLayout中自定义NavigationView以获得这样的UI。 - Jeel Vankhede
5个回答

7

我成功地实现了你想要的关于BottomNavigationView的功能,通过在方向改变时进行一些调整和移动一些视图。首先,为了在方向改变时进行操作,我在AndroidManifest中的Activity标签中添加了以下内容:

android:configChanges="orientation|screenSize"

在我的Activity中,我添加了以下内容:
 @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手机上得到的工作结果,你可以看一下截图。
截图: 横屏 竖屏

1

由于不能自定义BottomNavigation以垂直方式使用,建议(如果您仍未能解决此问题)切换到此库:SideMenu-Android 或者您可以创建一个自定义的导航抽屉。


0

很抱歉,BottomNavigationView 只能处理页面底部的导航,您应该创建自己的自定义抽屉或使用其中一个菜单 在此链接中 此外,如果这些都没有帮助到您,您可以查看此文档以了解如何处理不同的布局。

希望这可以帮到您。


0
   <item 
    android:id="@+id/text"
    android:title="@string/about"
    android:icon="@android:drawable/ic_menu_info_details"
    app:showAsAction="never"/>

app:showAsAction="never"放在每个菜单项中并尝试...

我已经更新了我的问题。工具栏菜单不是我的问题。我想让我的底部导航视图在平板电脑 - 横向模式下像上面的图片一样移动到活动的左侧。 - Anbarasu Chinna
尚未解决。仅为移动设备和平板电脑使用底部导航。 - Anbarasu Chinna

0

你不能将BottomNavigationView移动到布局的左侧区域。

BottomNavigationView不符合平板电脑的材料设计规范

唯一的方法是在启动时检查屏幕方向,例如:

if (islandcape()){

//Create the Navigation view layout here.. 

}else{

//Create the BottomNavigationView layout here .. 

}

注意:您需要编写一个函数来检查方向并替换上面代码中的 islandcape()
另外,尝试使用下面的库来完成这项工作。
Iiro Krankka https://github.com/roughike/BottomBar

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接