安卓:preference片段与导航抽屉的片段 [英] Android: Preference Fragment with a Navigation Drawer's Fragment

查看:204
本文介绍了安卓:preference片段与导航抽屉的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个已经使用了导航抽屉一个Android应用程序。 我的 MainActivity延伸片段活动我的 SettingFragment扩展preferenceFragment

Hi I have an Android app that already uses a Navigation Drawer. My MainActivity extends Fragment Activity and my SettingFragment extends PreferenceFragment

设置片段

public class SettingsFragment extends PreferenceFragment {
    public SettingsFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.prefs);
    }
}

和我的 MainActivity

PreferenceFragment preferenceFragment = new SettingsFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(android.R.id.content, preferenceFragment); // I'm getting an error here should be Fragment not PreferenceFragment
ft.commit();

我如何可以提交或去SettingsFragment()?

How can I commit or go to the SettingsFragment()?

推荐答案

这为我工作。 只要记住,这code将与API LEVAL 11及更高版本。

This Worked for me. Just keep in mind that this code will work with api leval 11 and higher.

在活动使用code添加片段。

In Activity use this code to add Fragment.

android.app.Fragment infoFragment = new InfoFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(android.R.id.content, infoFragment);
ft.commit();

和你的preferenceFragment类将是这样的。

And Your PreferenceFragment class will be look like this.

public class InfoFragment extends PreferenceFragment 
{
 /**
  * The fragment argument representing the section number for this
  * fragment.
 */
 private static final String ARG_SECTION_NUMBER = "section_number";

 /**
  * Returns a new instance of this fragment for the given section
  * number.
  */

  public static android.app.Fragment newInstance(int sectionNumber) 
  {
    InfoFragment fragment = new InfoFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
  }

  public InfoFragment() 
  {

  }
}

这篇关于安卓:preference片段与导航抽屉的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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