1
我正在開發一些應用程序與三個選項卡的ActionBar,與之一是設置選項卡。 PreferencesFragment加載我的偏好xml文件,所以我如何添加PreferenceFragment
到ActionBar
Tab?安卓喜好屏幕裏面的動作欄選項卡
截至本picteur:
這是添加了三個選項卡的主要活動,我增加了兩個簡單的片段標籤successfuly但一個問題開始與RreferenceFragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = this.getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab settings = actionBar.newTab();
settings.setText("Settings");
settings.setTabListener(new SettingsFragment());
actionBar.addTab(settings);
Tab control = actionBar.newTab();
control.setText("Controller");
control.setTabListener(new ControlFragment());
actionBar.addTab(control);
Tab information = actionBar.newTab();
information.setText("information");
information.setTabListener(new InformationFragment());
actionBar.addTab(information);
}
例如,這是一個ControlFragment類(第二個標籤)
public class ControlFragment extends Fragment implements ActionBar.TabListener {
private Fragment fragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.control_fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction frgTransaction) {
//TODO
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction frgTransaction) {
fragment = new ControlFragment();
frgTransaction.add(android.R.id.content, fragment);
frgTransaction.attach(fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction frgTransaction) {
frgTransaction.remove(fragment);
}
}
這是一個SettingsFragment類做的主要問題
public class SettingsFragment extends PreferenceFragment implements ActionBar.TabListener {
private Activity act;
private Context context;
private Fragment fragment;
public SettingsFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_settings);
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
fragment = new SettingsFragment();
arg1.add(android.R.id.content, fragment);
arg1.attach(fragment);
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
應用程序崩潰,因爲它的一個問題辦理PreferenceFragment
。
我該如何實現它?我知道TabHost,但我使用Action Bar。
嗨,你解決了嗎? – Mohammad