【问题标题】:How to change the toolbar when I change from an activity to a fragment当我从活动更改为片段时如何更改工具栏
【发布时间】:2019-04-09 16:49:45
【问题描述】:

我有一个 Activity、一个 Toolbar 和几个 Fragment,如何更改从 MainActivity 传递给 Fragment 的工具栏

我尝试从 fragment1.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
                        }

这是我想要更改 ActionBar 的片段的 XML,但每当我添加 layout 标签时,它都会添加另一个 Actionbar 示例

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

【问题讨论】:

  • 请添加 XML 文件
  • @CesarJoelGurrola 检查我的答案,我还给出了一个关于布局顺序的示例,以及如何更新片段中的标题。

标签: android android-fragments android-activity kotlin android-toolbar


【解决方案1】:

在您的活动中

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

在你的片段中

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

您的 Activity 布局应该遵循以下示例中的顺序

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

【讨论】:

  • 没有工作我试过(活动为?AppCompatActivity)?.setSupportActionBar(findViewById(工具栏)),任何cmets?问候
  • 我可以知道您为什么要在片段中再次设置工具栏吗?仅在 MainActivity 内设置工具栏,它会添加/替换片段。让片段只更新标题,而不是设置工具栏,这将防止显示两个工具栏一个在另一个之上
  • 这是因为我需要将其更改为 (backToolbar) 而不是菜单之一,我需要: - 在 Main Activity -> MenuToolbar - 在 Fragment1 -> BackToolbar 知道如何实现这一点吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多