从 android 中的片段更改自定义 ActionBar 标题
change custom ActionBar title from fragment in android
你好,在下面的代码中我有一个 fab 按钮,如果我按下 fab 按钮用另一个片段替换一个片段,同时我也在更改标题。
但是片段被替换了,但是标题没有改变。
谁能帮帮我
OneFragement.java:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the borderdashboard for this fragment
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Task List");
View rootView = inflater.inflate(R.layout.account_list, container, false);
setHasOptionsMenu(true);
setSearchtollbar();
FloatingActionButton fb = rootView.findViewById(R.id.fab);
fb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isDuplicate = "false";
Bundle args = new Bundle();
args.putString("isDuplicate", String.valueOf(isDuplicate));
fragment = new TaskCreateFragement();
sessionId = getActivity().getIntent().getStringExtra("sessionId");
fragment.setArguments(args);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Create New Task");
loadFragment(fragment);
}
});
SecondFragment.java:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the borderdashboard for this fragment
((AppCompatActivity) getContext()).getSupportActionBar().setTitle("Create New Task");
MainActivity.java:
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// getSupportActionBar().setTitle("DASHBOARD");
final ActionBar ab = getSupportActionBar();
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ab.setCustomView(R.layout.toolbar_spinner);
if (ab != null) {
ab.setDisplayShowTitleEnabled(false);
mTitle = (TextView) findViewById(R.id.toolbar_title);
mTitle.setText("DASHBOARD");
mTitle.setGravity(Gravity.CENTER_HORIZONTAL);
// Typeface typeface = Typeface.createFromAsset(getApplicationContext ().getAssets (), "fonts/astype - Secca Light.otf");
// mTitle.setTypeface (typeface);
}
ab.setHomeAsUpIndicator(R.drawable.menu);
ab.setDisplayHomeAsUpEnabled(true);
在托管您的片段的 activity 中的 onCreate
方法中添加此片段,并查看您的代码是否正常工作。
@Override
protected void onCreate(Bundle savedInstanceState) {
getActionBar().setDisplayShowTitleEnabled(true);
在您的 Activity
中创建方法
public void setActionBarTitle(String title) {
mTitle.setText(title); // as you used custom view
}
在 SecondFragment
中更改 onCreate
或 onResume
中的标题
((YourActivity)getActivity()).setActionBarTitle("Create New Task");
进行回调class
package com.yourpackage.view;
public interface HomePageBottomCallback extends IView {
void onTitle(String Title);
}
package com.yourpackage.view;
import android.content.Context;
public interface IView {
Context getContext();
}
使用 HomePageBottomCallback 实现您的主 class 并在主 class
中实现其方法
public class MainActivity extends BaseActivity
implements HomePageBottomCallback{
}
@Override
public void onTitle(String Title) {
TitleSet(Title);
setTitle(Title)
}
在 FRAGMENT CAllback 中使用
public HomePageBottomCallback callback;
@Override
public void onAttach( Context context) {
super.onAttach(context);
try {
callback = (HomePageBottomCallback) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement HeadlineListener");
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
callback.onTitle("Your Page");
}
您正在工具栏中创建自定义视图,因此您必须更改 Toolbar
布局中 TextView
的文本。
在您的 Activity
中创建一个方法
public void setActionBarTitle(String title) {
mTitle.seText(title);
}
在 SecondFragment
中更改 onCreate
或 onResume
中的标题
((YourActivityClassName)getActivity()).setActionBarTitle("Create New Task");
你好,在下面的代码中我有一个 fab 按钮,如果我按下 fab 按钮用另一个片段替换一个片段,同时我也在更改标题。
但是片段被替换了,但是标题没有改变。
谁能帮帮我
OneFragement.java:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the borderdashboard for this fragment
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Task List");
View rootView = inflater.inflate(R.layout.account_list, container, false);
setHasOptionsMenu(true);
setSearchtollbar();
FloatingActionButton fb = rootView.findViewById(R.id.fab);
fb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isDuplicate = "false";
Bundle args = new Bundle();
args.putString("isDuplicate", String.valueOf(isDuplicate));
fragment = new TaskCreateFragement();
sessionId = getActivity().getIntent().getStringExtra("sessionId");
fragment.setArguments(args);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Create New Task");
loadFragment(fragment);
}
});
SecondFragment.java:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the borderdashboard for this fragment
((AppCompatActivity) getContext()).getSupportActionBar().setTitle("Create New Task");
MainActivity.java:
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// getSupportActionBar().setTitle("DASHBOARD");
final ActionBar ab = getSupportActionBar();
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ab.setCustomView(R.layout.toolbar_spinner);
if (ab != null) {
ab.setDisplayShowTitleEnabled(false);
mTitle = (TextView) findViewById(R.id.toolbar_title);
mTitle.setText("DASHBOARD");
mTitle.setGravity(Gravity.CENTER_HORIZONTAL);
// Typeface typeface = Typeface.createFromAsset(getApplicationContext ().getAssets (), "fonts/astype - Secca Light.otf");
// mTitle.setTypeface (typeface);
}
ab.setHomeAsUpIndicator(R.drawable.menu);
ab.setDisplayHomeAsUpEnabled(true);
在托管您的片段的 activity 中的 onCreate
方法中添加此片段,并查看您的代码是否正常工作。
@Override
protected void onCreate(Bundle savedInstanceState) {
getActionBar().setDisplayShowTitleEnabled(true);
在您的 Activity
public void setActionBarTitle(String title) {
mTitle.setText(title); // as you used custom view
}
在 SecondFragment
中更改 onCreate
或 onResume
((YourActivity)getActivity()).setActionBarTitle("Create New Task");
进行回调class
package com.yourpackage.view;
public interface HomePageBottomCallback extends IView {
void onTitle(String Title);
}
package com.yourpackage.view;
import android.content.Context;
public interface IView {
Context getContext();
}
使用 HomePageBottomCallback 实现您的主 class 并在主 class
中实现其方法public class MainActivity extends BaseActivity
implements HomePageBottomCallback{
}
@Override
public void onTitle(String Title) {
TitleSet(Title);
setTitle(Title)
}
在 FRAGMENT CAllback 中使用
public HomePageBottomCallback callback;
@Override
public void onAttach( Context context) {
super.onAttach(context);
try {
callback = (HomePageBottomCallback) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement HeadlineListener");
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
callback.onTitle("Your Page");
}
您正在工具栏中创建自定义视图,因此您必须更改 Toolbar
布局中 TextView
的文本。
在您的 Activity
public void setActionBarTitle(String title) {
mTitle.seText(title);
}
在 SecondFragment
中更改 onCreate
或 onResume
((YourActivityClassName)getActivity()).setActionBarTitle("Create New Task");