如何更改底部导航栏的文本位置?

时间:2018-11-14 10:15:44

标签: android

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

like this in image

这是我的代码:

 <?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

谢谢。

3 个答案:

答案 0 :(得分:0)

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

app:labelVisibilityMode="labeled"
  

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

答案 1 :(得分:0)

使用此:

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更改为您喜欢的内容。

答案 2 :(得分:0)

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>