Текст пункта меню не отображается в BottomAppBar - PullRequest
0 голосов
/ 13 марта 2019

Я пытался добавить заголовок пункта меню вместе со значком меню на нижней панели приложений в Android Studio. Я пытался установить:

app:showAsAction="ifRoom|withText"

Но это не сработало. Только значок отображается. Я также попытался создать свой собственный файл макета и использовать actionLayout:

        android:actionLayout="@layout/menu_item_lists"

это тоже не сработало.

Пример того, что я хочу (с текстом под значками):

Как это выглядит сейчас:

enter image description here

XML для моей деятельности:

<?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:id="@+id/activity_main_constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed"
    tools:context=".MainActivity">


    <fragment
        android:id="@+id/activity_main_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation_graph" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            style="@style/Widget.MaterialComponents.BottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#ffffff"
            app:fabAlignmentMode="end"
            app:fabCradleMargin="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            app:backgroundTint="@color/colorPrimary"
            app:layout_anchor="@id/bottomAppBar"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.502"
            app:layout_constraintStart_toStartOf="parent"
            app:rippleColor="@color/colorAccent"
            app:srcCompat="@drawable/ic_add_white_24dp" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

И код для моего меню:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/fragment_menu_lists"
        android:title="Handlelister"
        android:icon="@drawable/ic_format_list_bulleted_black_24dp"
        android:actionLayout="@layout/menu_item_lists"
        app:showAsAction="ifRoom|withText" />
    <item android:id="@+id/fragment_menu_stores"
        android:title="Butikker"
        android:icon="@drawable/ic_map_black_24dp"
        app:showAsAction="ifRoom|withText"/>
    <item android:id="@+id/fragment_menu_profile"
        android:title="Profil"
        android:icon="@drawable/ic_person_outline_black_24dp"
        app:showAsAction="ifRoom|withText"
    />
</menu>

Код, в котором я пытался создать свой собственный макет действия, если это уместно:

<?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:id="@+id/relativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="28dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_format_list_bulleted_black_24dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Handlelister"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

Ответы [ 2 ]

1 голос
/ 13 марта 2019

Это поведение по умолчанию BottomAppBar, чтобы сделать текст видимым, поместите атрибут BottomAppBar ниже

app:labelVisibilityMode="labeled"
0 голосов
/ 13 марта 2019

Я нашел способ сделать это в конце концов:

Вместо использования

android:actionLayout="@layout/menu_item_lists"

Я использовал:

app:actionLayout="@layout/menu_item_lists"

Затем мне пришлось добавить onClickListener напункты меню:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.menu_bottom_navigation, menu)

        val navController = findNavController(R.id.activity_main_nav_host_fragment)

        menu?.findItem(R.id.fragment_menu_lists)?.actionView?.setOnClickListener {
            navController.navigate(R.id.fragment_menu_lists)
        }

        menu?.findItem(R.id.fragment_menu_stores)?.actionView?.setOnClickListener {
            navController.navigate(R.id.fragment_menu_stores)
        }

        menu?.findItem(R.id.fragment_menu_profile)?.actionView?.setOnClickListener {
            navController.navigate(R.id.fragment_menu_profile)
        }

        return super.onCreateOptionsMenu(menu)
    }
...