Как изменить панель инструментов при переходе от действия к фрагменту

1

У меня есть Activity, панель инструментов и несколько фрагментов, как я могу изменить панель инструментов, переданную фрагменту из MainActivity

я пытался вызвать панель инструментов из фрагмента. XML, но он показывает две панели инструментов друг над другом

Это где я назвал фрагмент

        R.id.menu_acercade -> {
                    binding.navegacionInferior.selectedItemId = item.itemId
                    cerrarDrawer()
                    false
                }
        R.id.menu_acercade -> {
                            intercalarIconosMenu(false)
                            intercalarPagers(true)
                            binding.navegacionInferior.menu.getItem(2).isCheckable = true  //binding.navegacionInferior.menu.getItem(2).isCheckable = true
                            binding.viewpagerSecciones.setCurrentItem(0, true) //antes 0
                            true
                        }

Вот XML фрагмент, в котором я хочу изменить ActionBar, но всякий раз, когда я добавляю тэг layou, он добавляет другой пример Actionbar

два ActionBars

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--<include layout="@layout/toolbar_atras" />-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="542dp"
            android:orientation="vertical">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="24dp"
                android:src="@drawable/about_logo" />
            <android.support.v7.widget.AppCompatTextView
                style="@style/Titulo.Principal"
                android:layout_marginBottom="24dp"
                android:text="@string/menu_opcion_acercade" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="36dp"
                android:layout_marginRight="36dp"
                android:layout_marginBottom="16dp"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
                android:textColor="@color/colorTexto" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="36dp"
                android:layout_marginRight="36dp"
                android:text="Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                android:textColor="@color/colorTexto" />
        </LinearLayout>


        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navegacionInferior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginBottom="0dp"
            android:background="@color/colorBlanco"
            android:paddingEnd="0dp"
            android:paddingBottom="0dp"
            app:elevation="16dp"
            app:itemBackground="@drawable/fondo_navegacioninferior"
            app:itemIconTint="@color/colorPrimario"
            app:itemTextColor="@color/colorPrimario"
            tools:menu="@menu/menu_inferior" />
    </LinearLayout>
</layout>
Теги:
kotlin
android-fragments
android-activity
android-toolbar

1 ответ

0

Внутри вашей деятельности

setSupportActionBar(yourToolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)

В вашем фрагменте

(activity as? AppCompatActivity)?.supportActionBar?.title = "Your Title"

Ваш макет активности, вероятно, должен следовать последовательности в приведенном ниже примере

<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- AppBarLayout is a wrapper for a Toolbar -->
<!-- Note that AppBarLayout expects to be the first child nested within a CoordinatorLayout -->
<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>


<!-- FrameLayout can be used to insert fragments to display the content of the screen -->

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</android.support.design.widget.CoordinatorLayout>
  • 0
    не работает, я пытался (активность как? AppCompatActivity)?. setSupportActionBar (findViewById (панель инструментов)), какие-либо комментарии? поздравил
  • 0
    Могу ли я узнать, почему вы снова настраиваете панель инструментов внутри своего фрагмента? Установите панель инструментов только внутри вашей MainActivity, которая добавляет / заменяет фрагмент. Позвольте фрагменту просто обновить заголовок, вместо настройки панели инструментов, это предотвратит показ двух панелей инструментов одна над другой
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню