gpt4 book ai didi

android - 如何将工具栏添加到android中的 fragment ?

转载 作者:行者123 更新时间:2023-12-05 00:11:24 26 4
gpt4 key购买 nike

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

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

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

getActivity().setSupportActionController(toolbar)

错误是:

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

而且有一个条件,我不能将我的HomeFragment 扩展到AppCombatActivity。它必须仅保留为 Fragment,即 public class HomeFragment extends Fragment

条件是因为这段代码中的第5行

    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() 是:

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

所以如果我将public class HomeFragment extends Fragment更改为public class HomeFragment extends AppCombatActivity,上面代码中的返回语句将给出不兼容类型错误 .

请帮助我解决这个错误,因为这个错误我无法创建工具栏或操作栏。任何帮助将不胜感激。谢谢!

最佳答案

首先将您的 Activity 从 getActivity() 转换为 AppCompatActivity。我认为这将解决您的错误:

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

或者在你的情况下:

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

getActivity() 返回一个 FragmentActivity,您需要返回 AppCompatActivity。希望这对您有所帮助!

关于android - 如何将工具栏添加到android中的 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41091438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com