我正在創建一個應用程序,其中我使用偏好片段與導航抽屜我的問題是我創建導航抽屜和其中我添加了一些菜單項,我想要當應用程序加載時,加載首選項Fragment作爲第一個元素。它不會顯示。當我改變選項,即當我改變抽屜菜單選項像第二個元素在抽屜菜單拳頭視圖不替換第二個,它是重疊的第一個如何避免片段與Android導航抽屜重疊
我的導航抽屜xml是遵循
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.koteswara.wise.NavigationDrawerMainActivity" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start" >
<ListView
android:id="@+id/drawer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
tools:context="com.koteswara.wise.NavigationDrawerFragment" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我的偏好片段就像是被添加加載PreferenceFragment這
public class AutoAnswerPreferenceActivity extends PreferenceFragment implements OnSharedPreferenceChangeListener {
private AutoAnswerNotifier mNotifier;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
mNotifier = new AutoAnswerNotifier(getActivity());
mNotifier.updateNotification();
SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onDestroy() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("enabled")) {
mNotifier.updateNotification();
}
}
}
我的片段類是
public class AutoAnswarFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getActivity().getFragmentManager().beginTransaction().replace(R.id.container, new AutoAnswerPreferenceActivity()) .commit();
}
在顯示出片段的方法是
protected void showFragment(int position) {
// TODO Auto-generated method stub
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AutoAnswarFragment();
break;
case 1:
fragment = new Tab2();
break;
case 2:
// fragment = new BhajanaFragment();
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
// update selected item and title, then close the drawer
// mDrawerList.setItemChecked(position, true);
// mDrawerList.setSelection(position);
mTitle = mDrawer_title[position];
// mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
// Log.e("MainActivity", "Error in creating fragment");
}
}
}
如果我會打電話比偏好碎片像下方之外的片段導航抽屜類將作品請幫我
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
}
我是努力澄清解決方案,請它任何機構解決它,並請告訴我的解決方案。我將感謝滿請幫助我
即時消息也爲此尋找一個完美的答案,但還沒有得到。但我已經做了一個小竅門,我只是把白色的背景顏色給我的佈局。問題解決了。 –
我在xml中使用框架佈局是我需要使框架佈局的背景爲白色 –
是的。我這樣做,並暫時解決它。但實際上它不是正確的方式。 –