【问题标题】:How to change bottom navigation bar text position?如何更改底部导航栏文本位置?
【发布时间】:2018-11-14 20:03:15
【问题描述】:

我想改变底部导航栏中的文本位置,如下图所示,

这是我的代码:

 <?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity">

    <android.support.design.widget.BottomNavigationView

        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:theme="@style/Widget.BottomNavigationView"
        android:layout_alignParentStart="false"
        android:layout_alignParentEnd="false"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="0dp"
        android:background="#fcff38"
        app:itemIconSize="30dp"
        app:itemTextColor="#000000"


        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menulist" />

</android.widget.RelativeLayout> 

使用 android studio 3.2

谢谢。

【问题讨论】:

  • 我想改变文本位置你能解释一下你想要什么吗?该图像显示了一个常规的底部导航视图。
  • 我希望文本有更多的图标填充
  • 修正语法和格式
  • 好的英语科学

标签: android


【解决方案1】:

res/layout/activlty_main.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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".HomeActivity">

    <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fcff38"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@color/white"
            app:itemTextColor="#000000"
            app:menu="@menu/menulist"/>

</android.support.constraint.ConstraintLayout>

res/menu/menulist.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:id="@+id/navigation_home"
            android:icon="@drawable/home"
            android:title="Home"/>

    <item
            android:id="@+id/navigation_gallery"
            android:icon="@drawable/ic_album"
            android:title="Gallery"/>

    <item
            android:id="@+id/navigation_food"
            android:icon="@drawable/ic_food"
            android:title="Foods"/>
    <item
            android:id="@+id/navigation_schedule"
            android:icon="@drawable/ic_schedule"
            android:title="Schedule"/>

</menu>

【讨论】:

    【解决方案2】:

    您需要将标签可见性模式设置为标签。

    app:labelVisibilityMode="labeled"
    

    标签可见性模式决定是否显示或隐藏标签 导航项。将标签可见性模式设置为 LABEL_VISIBILITY_SELECTED 将标签设置为仅在选中时显示,将其设置为 LABEL_VISIBILITY_LABELED 将标签设置为 始终显示,并且 LABEL_VISIBILITY_UNLABELED 将标签设置为 从不显示。

    【讨论】:

    • 我想改变文字位置,因为它离图标太近了
    • 您可以创建自定义菜单并使用它。
    【解决方案3】:

    使用这个:

    for (int i = 0; i < bottomNavigationView.getChildCount(); i++) {
        View item = bottomNavigationView.getChildAt(i);
    
        if (item instanceof BottomNavigationMenuView) {
            BottomNavigationMenuView menu = (BottomNavigationMenuView) item;
    
            for (int j = 0; j < menu.getChildCount(); j++) {
                View menuItem = menu.getChildAt(j);
    
                View small = menuItem.findViewById(android.support.design.R.id.smallLabel);
                if (small instanceof TextView) {
                    ((TextView) small).setPadding(0, 20, 0, 0);
                }
                View large = menuItem.findViewById(android.support.design.R.id.largeLabel);
                if (large instanceof TextView) {
                    ((TextView) large).setPadding(0, 20, 0, 0);
                }
            }
        }
    }
    

    将 textview 和 BottomNavigationView 的顶部填充设置为您的 bottomNavigationView id。
    20改成你喜欢的。

    【讨论】:

      猜你喜欢
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      相关资源
      最近更新 更多