从 android 中的片段更改自定义 ActionBar 标题

问题描述 投票:0回答:5

您好,在下面的代码中,如果我按下 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);
android android-fragments android-actionbar
5个回答
1
投票

您正在工具栏中创建自定义视图,因此您必须更改

TextView
布局内
Toolbar
的文本。

在您的

Activity

中创建一个方法
public void setActionBarTitle(String title) {
    mTitle.seText(title);
}

SecondFragment
中更改标题
onCreate
onResume

((YourActivityClassName)getActivity()).setActionBarTitle("Create New Task");

0
投票

在您的

Activity

中创建方法
public void setActionBarTitle(String title) {
    mTitle.setText(title);    // as you used custom view
}

SecondFragment
中更改标题
onCreate
onResume

((YourActivity)getActivity()).setActionBarTitle("Create New Task");

0
投票

将此代码段添加到托管片段的活动的

onCreate
方法中,并查看您的代码是否正常工作。

@Override
    protected void onCreate(Bundle savedInstanceState) {
getActionBar().setDisplayShowTitleEnabled(true);

0
投票

制作回调类

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");
        }

0
投票

在fragment的onCreate方法中将标题设置为

activity?.title = "Your title"

© www.soinside.com 2019 - 2024. All rights reserved.