android - 从android中的Fragment访问工具栏textview

标签 android android-fragments android-toolbar

我有带自定义工具栏的抽屉导航。我正在尝试访问 fragment 中工具栏中的 textView 并在 fragment 中设置文本。这可能吗?如果可能的话如何?

这是我的 toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/tools"
 android:id="@+id/toolbar"
 style="@style/ActionBar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@color/backgroundcolor"
 android:minHeight="?attr/actionBarSize"
 app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/relativeLayoutID">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:layout_centerVertical="true"
        android:text="@string/register_title"
        android:textColor="@color/blackText"
        android:textSize="@dimen/text_size_medium" />

    <ImageView
        android:id="@+id/action_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="right|center"
        android:contentDescription="@string/logo"
        android:paddingRight="5dp"
        android:src="@drawable/logoactionbar" />
</RelativeLayout>

我想根据我点击的 fragment 设置 toolbar_title 的文本。我如何在 Fragment 中访问它们。

告诉我!

谢谢!

最佳答案

请试试这个

toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
textToolHeader = (TextView) toolbar.findViewById(R.id.textToolHeader);
textToolHeader.setText("Text which you want");

希望对您有所帮助。

关于android - 从android中的Fragment访问工具栏textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31428086/

相关文章:

java - Android: View 寻呼机中 fragment 之间的通信

android - 如何使 colorPrimary 不显示为工具栏背景

android.support.v7.widget.Toolbar 图标对齐问题

android - 无法执行 dex : method ID not in [0, 0xffff]:65536

java - 当我尝试在 fragment 生命周期中实例化新类时出错

android - 有没有一种方法可以像结果的开始 fragment 一样工作?

android - 如何动态启用/禁用整个溢出菜单?

android - 在哪里放置AsyncTask

android - Android上OpenGL-ES如何绘制多条不同颜色的线段

android - 在不同的 Activity 或 Fragment 之间共享数据的正确方法是什么?