我有带自定义工具栏的抽屉导航。我正在尝试访问 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/