【问题标题】:How to create vertical BottomNavigationView android?如何创建垂直的BottomNavigationView android?
【发布时间】:2018-03-12 11:57:28
【问题描述】:

我有一个活动,顶部有 4 个片段,底部导航视图底部有 4 个项目。它在移动设备中运行良好。当我使用 LANDASCAPE 购买平板电脑时,我想将 BottomNavigationView 移动到活动的左侧,垂直方向如下所示。

这是否可以使用 BottomNavigationView 实现,或者我应该使用 NavigationMenu android。

【问题讨论】:

  • 使用bottomNavigationMenu,你无法做到这一点,但你可以试试这个:stackoverflow.com/a/29809691/5479863
  • 您可以在DrawerLayout 中自定义NavigationView 以获得这样的用户界面。

标签: android android-layout android-fragments


【解决方案1】:

嗯,我设法做到了,你想要的 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>

当然,您必须根据您的手机需要更改 navigation.getLayoutParams().width、setX 和 setY。您可以创建一个函数来计算位置。抱歉,今天是 10:30,整晚都在编码,所以要休息,所以无法编写该函数。但如果你愿意,我会写那个函数。这是我为我的手机 Vivo Y51L 工作的,你可以看看截图。

截图

【讨论】:

    【解决方案2】:

    由于您无法自定义 BottomNavigation 以垂直使用它。我建议(如果您仍然没有设法修复它)切换到此库:SideMenu-Android 或者您可以创建一个自定义的导航抽屉。

    【讨论】:

      【解决方案3】:

      恐怕BottomNavigationView 只处理页面导航的底部,您应该创建自己的自定义抽屉或使用菜单之一in this link 如果这些对您没有帮助,您还可以查看this documentation 了解如何处理不同的布局

      希望对你有帮助

      【讨论】:

        【解决方案4】:
           <item 
            android:id="@+id/text"
            android:title="@string/about"
            android:icon="@android:drawable/ic_menu_info_details"
            app:showAsAction="never"/>
        

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

        【讨论】:

        • 我已经更新了我的问题。工具栏菜单不是我的问题。我希望我的底部导航视图应该移动到活动的左侧,如上图所示,仅适用于平板电脑 - 横向模式
        • 尚未解决。只是在移动设备和平板电脑上都使用了底部导航。
        【解决方案5】:

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

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

        唯一的方法是你需要在开始时检查屏幕方向

        if (islandcape()){
        
        //Create the Navigation view layout here.. 
        
        }else{
        
        //Create the BottomNavigationView layout here .. 
        
        }
        

        注意:你需要编写一个函数来检查方向并替换上面代码中的islandcape()

        也可以试试下面的库,它可以很好地完成这项工作。

        饭郎克兰卡https://github.com/roughike/BottomBar

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-20
          • 1970-01-01
          • 2014-10-31
          • 1970-01-01
          • 2014-11-30
          • 1970-01-01
          相关资源
          最近更新 更多