首页 > 解决方案 > Android底部导航视图在图标侧面显示文本

问题描述

我想在图标右侧显示底部栏文本,例如 City Mapper。我目前使用 Android 默认组件实现了我的第一个版本

City Mapper底部导航

在这里提出了这个问题,但没有找到解决方案,并且有人评论说这是不可能的。如果 City Mapper 正在这样做,那么它应该是可能的。我该怎么做?

标签: androidandroid-jetpackandroid-bottomnavigationview

解决方案


如何处理

我认为通过创建自己的视图或片段来处理这个问题会更容易。例如,您可以创建一个片段,并在此片段的布局中根据需要显示图标和文本。它会给你更多的权力来定制它。

其他人正在使用什么

我们不知道“城市事务”使用什么方式来提供这个底栏,也许他们已经实现了他们自己的自定义视图。

这是一个简单的活动示例,您可以根据需要使用底栏: 带有使用线性布局创建的底栏的简单活动。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="#BFBFBF"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <LinearLayout
        android:background="#FFF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@android:drawable/ic_menu_search" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Search" />

        </LinearLayout>

        <View
            android:layout_width="1dp"
            android:background="#ACACAC"
            android:layout_marginVertical="5dp"
            android:layout_height="match_parent"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@android:drawable/ic_menu_search" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Search" />

        </LinearLayout>

    </LinearLayout>
    
</androidx.constraintlayout.widget.ConstraintLayout>

我希望它会帮助你!


推荐阅读