Как изменить панель инструментов при переходе от действия к фрагменту - PullRequest
1 голос
/ 09 апреля 2019

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

Я пытался вызвать панель инструментов из фрагмента1.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, он добавляет другойПример панели действий

две панели действий

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

1 Ответ

0 голосов
/ 09 апреля 2019

Внутри вашей активности

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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...