简体   繁体   English

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

[英]change custom ActionBar title from fragment in android

Hi in the below code I have a one fab button if I press the fab button replacing one fragment with another fragment in the meanwhile I am changing the title also.嗨,在下面的代码中,如果我按下 fab 按钮用另一个片段替换一个片段,同时我也在更改标题,我有一个 fab 按钮。

But fragments are replaced but the title is not changing.但是片段被替换了,但标题没有改变。

Can anyone help me谁能帮我

OneFragement.java: 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: 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: 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);

add this snippet in the onCreate method in the activity where your fragments are hosted and see if your code is working.在托管您的片段的活动中的onCreate方法中添加此代码段,并查看您的代码是否正常工作。

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

Create method in your Activity在您的Activity中创建方法

public void setActionBarTitle(String title) {
    mTitle.setText(title);    // as you used custom view
}

In SecondFragment change title in onCreate or onResumeonCreateonResume中的SecondFragment更改标题

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

Make Callback class进行回调 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();


}

implements with HomePageBottomCallback to your main class and implement its method in main class使用 HomePageBottomCallback 实现您的主 class 并在主 class 中实现其方法

public class MainActivity extends BaseActivity
        implements HomePageBottomCallback{

        }

         @Override
    public void onTitle(String Title) {

        TitleSet(Title);
        setTitle(Title)

    }

in FRAGMENT CAllback used在片段回调中使用

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

You are creating a custom view in your toolbar, so you have to change the text of the TextView inside Toolbar layout.您正在工具栏中创建自定义视图,因此您必须在Toolbar布局中更改TextView的文本。

Create a method in your Activity在您的Activity中创建一个方法

public void setActionBarTitle(String title) {
    mTitle.seText(title);
}

In SecondFragment change title in onCreate or onResumeonCreateonResume中的SecondFragment更改标题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM