在 android 中从 Fragment 访问工具栏文本视图
Access toolbar textview from Fragment in android
我有带自定义工具栏的导航抽屉。我正在尝试访问片段中工具栏中的 textView 并在片段中设置文本。这可能吗,如果可能的话如何?
这是 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>
我想根据点击的片段设置toolbar_title的文字。如何在 Fragment 中访问它们。
告诉我!
谢谢!
请试试这个
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
textToolHeader = (TextView) toolbar.findViewById(R.id.textToolHeader);
textToolHeader.setText("Text which you want");
希望对您有所帮助。
在你的 activity
中创建 public textChanger()
方法
public class YourActiviy extends Activity {
Toolbar toolbar;
Textview text;
textToolHeader.setText("Text which you want");
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
text = (TextView) toolbar.findViewById(R.id.text);
}
public void changeToolBarText(String txt){
text.setText(txt);
}
}
并在您的片段中使用
((YourActiviy) getActivity()).changeToolBarText("what ever you want");
更改该文本视图。
作为样本,片段;
public class test extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
((YourActivity) getActivity()).changeToolBarText("what ever you want");
return super.onCreateView(inflater, container, savedInstanceState);
}
}
目前这对我有用。只需调用 onCreateView
:
AppBarLayout toolbar = (AppBarLayout) getActivity().findViewById(R.id.toolbar);
TextView topBack = (TextView) toolbar.findViewById(R.id.back);
我有带自定义工具栏的导航抽屉。我正在尝试访问片段中工具栏中的 textView 并在片段中设置文本。这可能吗,如果可能的话如何?
这是 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>
我想根据点击的片段设置toolbar_title的文字。如何在 Fragment 中访问它们。
告诉我!
谢谢!
请试试这个
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
textToolHeader = (TextView) toolbar.findViewById(R.id.textToolHeader);
textToolHeader.setText("Text which you want");
希望对您有所帮助。
在你的 activity
中创建public textChanger()
方法
public class YourActiviy extends Activity {
Toolbar toolbar;
Textview text;
textToolHeader.setText("Text which you want");
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
text = (TextView) toolbar.findViewById(R.id.text);
}
public void changeToolBarText(String txt){
text.setText(txt);
}
}
并在您的片段中使用
((YourActiviy) getActivity()).changeToolBarText("what ever you want");
更改该文本视图。
作为样本,片段;
public class test extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
((YourActivity) getActivity()).changeToolBarText("what ever you want");
return super.onCreateView(inflater, container, savedInstanceState);
}
}
目前这对我有用。只需调用 onCreateView
:
AppBarLayout toolbar = (AppBarLayout) getActivity().findViewById(R.id.toolbar);
TextView topBack = (TextView) toolbar.findViewById(R.id.back);