您好,在下面的代码中,如果我按下 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);
您正在工具栏中创建自定义视图,因此您必须更改
TextView
布局内 Toolbar
的文本。
在您的
Activity
中创建一个方法
public void setActionBarTitle(String title) {
mTitle.seText(title);
}
在
SecondFragment
中更改标题 onCreate
或 onResume
((YourActivityClassName)getActivity()).setActionBarTitle("Create New Task");
在您的
Activity
中创建方法
public void setActionBarTitle(String title) {
mTitle.setText(title); // as you used custom view
}
在
SecondFragment
中更改标题 onCreate
或 onResume
((YourActivity)getActivity()).setActionBarTitle("Create New Task");
将此代码段添加到托管片段的活动的
onCreate
方法中,并查看您的代码是否正常工作。
@Override
protected void onCreate(Bundle savedInstanceState) {
getActionBar().setDisplayShowTitleEnabled(true);
制作回调类
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 并在主类中实现其方法
public class MainActivity extends BaseActivity
implements HomePageBottomCallback{
}
@Override
public void onTitle(String Title) {
TitleSet(Title);
setTitle(Title)
}
在 FRAGMENT CCallback 中使用
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");
}
在fragment的onCreate方法中将标题设置为
activity?.title = "Your title"