在Android中通过片段在工具栏中设置标题 [英] Set a title in Toolbar from fragment in Android

查看:85
本文介绍了在Android中通过片段在工具栏中设置标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用AppCompatv7 lib中的最新工具栏.我已经在工具栏视图组中放置了一个文本视图,我想从我的活动片段中为此文本视图设置一个标题.)getActivity).setcustomView(..)可以完成这项工作,但是由于使用了此工具栏,我无法使用它.此外,我还在BaseActivity中实现了一个由所有Activity继承的方法.初始化向左滑动抽屉的方法.我必须在活动中初始化initDrawerLayout()方法,否则不会初始化抽屉.如果我对其进行片段化初始化,这给了我所有空的结果,则抽屉的切换按钮和自定义标题也没有设置.

I have been using the latest Toolbar from AppCompatv7 lib.I have placed a textview in the ToolBar ViewGroup And I want to set a title into this Textview from the fragment in my activity.In case of a custom action bar ((ActionBarActivity)getActivity).setcustomView(..) would have done the job.But due to use of this ToolBar I am not able to use that.Also I have implemented a method in my BaseActivity that is inherited by all Activities.This BaseActivity contains my method to initialize a sliding drawer to the left.I have to initialize the initDrawerLayout() method in activity else the drawer would not be initialized.And if I initialize it in fragment its giving me all empty results,neither the toggle button for drawer and nor is the custom title getting set.

这是我的initDrawer代码.

This is my initDrawer code..

public void initDrawerLayout(String toolbar_text) {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerRelative = (RelativeLayout) findViewById(R.id.drawer_relative);
        if (mDrawerLayout != null) {
            findViewById(R.id.drawer_btn_a).setOnClickListener(this);
            findViewById(R.id.drawer_btn_b).setOnClickListener(this);
            findViewById(R.id.drawer_btn_c).setOnClickListener(this);
            findViewById(R.id.drawer_btn_d).setOnClickListener(this);
            findViewById(R.id.drawer_btn_e).setOnClickListener(this);
            findViewById(R.id.drawer_btn_f).setOnClickListener(this);
            findViewById(R.id.drawer_btn_g).setOnClickListener(this);
            findViewById(R.id.drawer_btn_h).setOnClickListener(this);
            findViewById(R.id.drawer_btn_i).setOnClickListener(this);
            findViewById(R.id.drawer_btn_j).setOnClickListener(this);
            findViewById(R.id.drawer_btn_k).setOnClickListener(this);
            findViewById(R.id.drawer_btn_l).setOnClickListener(this);
            findViewById(R.id.my_layout).setOnClickListener(this);





            Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
            toolbar.setBackground(getResources().getDrawable(R.drawable.icn_actionbar_background));
            TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
            mTitle.setText(toolbar_text);
            mTitle.setTypeface(Typeface.DEFAULT_BOLD);
            if (toolbar != null) {
                setSupportActionBar(toolbar);
                 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
            toolbar.setNavigationIcon(R.drawable.ic_drawer);

             mDrawerToggle = new ActionBarDrawerToggle(
                    this,  mDrawerLayout, toolbar,
                    R.string.drawer_open, R.string.drawer_close
                );
                mDrawerLayout.setDrawerListener(mDrawerToggle);


            toolbar.setNavigationOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                        mDrawerLayout.closeDrawer(Gravity.LEFT);
                    } else {
                        mDrawerLayout.openDrawer(Gravity.LEFT);
                    }
                }
            });
            mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                    GravityCompat.START);

            mDrawerLayout.setScrimColor(getResources().getColor(
                    android.R.color.transparent));
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);

        }
    }

这是我在片段中的代码.

And this my code in the fragment..

(((FirstActivity)getActivity()).initDrawerLayout(mFirst.name); 其中mFirst是Person类的对象

((FirstActivity) getActivity()).initDrawerLayout(mFirst.name); where mFirst is a object of class Person

和工具栏代码.

<android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize">
           <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="Toolbar Title"
                android:textColor="@color/action_text-color"
                android:textSize="18sp"
                android:textStyle="bold" /> 

            </android.support.v7.widget.Toolbar>

请帮助大家.

推荐答案

要允许Fragment与其Activity进行通信(以设置工具栏标题),您可以在Fragment类中定义一个接口,并在Activity中实现它如下所述:与其他片段通信.

To allow a Fragment to communicate up to its Activity (to set your Toolbar Title), you can define an interface in the Fragment class and implement it within the Activity as described here: Communicating with Other Fragments.

这篇关于在Android中通过片段在工具栏中设置标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆