如何在Android的片段中添加工具栏? [英] How to add Toolbar to a fragment in android?

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

问题描述

我有一个名为HomeFragment.java的片段,它扩展了Fragment类.

I have a fragment named as HomeFragment.java and it extends Fragment class.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.android.test.R;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;

public class HomeFragment extends Fragment {

    private Toolbar toolbar;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_profile,container,false);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        getActivity().setSupportActionController(toolbar);
        //setSupportActionBar(toolbar);
        return view;
    }
}

但是我在一行中遇到错误:

But I am getting error in the line:

getActivity().setSupportActionController(toolbar)

错误是:

Cannot resolve method 'setSupportActionController(android.support.v7.widget.Toolbar)'

并且有一个条件,我无法将我的 HomeFragment 扩展到 AppCombatActivity .它必须仅保留为 Fragment ,即公共类HomeFragment扩展Fragment .

And there is a condition that I can't extend my HomeFragment to AppCombatActivity. It has to be remained as Fragment only, i.e. public class HomeFragment extends Fragment.

条件是由于这段代码中的第5行

The condition is because of line number 5 in this piece of code

    Runnable mPendingRunnable = new Runnable() {
        @Override
        public void run() {
            // update the main content by replacing fragments
            Fragment fragment = getHomeFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                    android.R.anim.fade_out);
            fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
            fragmentTransaction.commitAllowingStateLoss();
        }
    };

    // If mPendingRunnable is not null, then add to the message queue
    if (mPendingRunnable != null) {
        mHandler.post(mPendingRunnable);
    }

    // show or hide the fab button
    toggleFab();

    //Closing drawer on item click
    drawer.closeDrawers();

    // refresh toolbar menu
    invalidateOptionsMenu();
}

我的getHomeFragment()是:

And my getHomeFragment() is:

    private Fragment getHomeFragment() {
    switch (navItemIndex) {
        case 0:
            // home
            HomeFragment homeFragment = new HomeFragment();
            return homeFragment;
        case 1:
            // photos
            PhotosFragment photosFragment = new PhotosFragment();
            return photosFragment;
        case 2:
            // movies fragment
            MoviesFragment moviesFragment = new MoviesFragment();
            return moviesFragment;
        case 3:
            // notifications fragment
            NotificationsFragment notificationsFragment = new NotificationsFragment();
            return notificationsFragment;

        case 4:
            // settings fragment
            SettingsFragment settingsFragment = new SettingsFragment();
            return settingsFragment;
        default:
            return new HomeFragment();
    }
}

因此,如果我将公共类HomeFragment扩展Fragment 更改为公共类HomeFragment扩展AppCombatActivity ,则上述代码中的return语句将给出不兼容的类型错误.

So if I change public class HomeFragment extends Fragment to public class HomeFragment extends AppCombatActivity, the return statement in the above code will give incompatible type error.

请帮助我解决此错误,由于该错误,我无法制作工具栏或操作栏. 任何帮助将是可观的. 谢谢!

Please help me in resolving this error, I can't make toolbar or actionbar because of this error. Any help will be appreciable. Thankyou!

推荐答案

首先将您的活动从getActivity()更改为AppCompatActivity.我认为这可以解决您的错误:

Cast your activity from getActivity() to AppCompatActivity first. I think this will solve your error:

((AppCompatActivity) getActivity()).getSupportActionBar(toolbar);

或者您的情况:

((AppCompatActivity) getActivity()).setSupportActionController(toolbar);

getActivity()返回FragmentActivity,您需要获取返回的AppCompatActivity.希望这会有所帮助!

getActivity() returns a FragmentActivity and you need to get AppCompatActivity returned. Hope this helps!

这篇关于如何在Android的片段中添加工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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