我设计了导航抽屉应用。除片段导航外,所有其他功能都正常工作。
当我尝试从侧面菜单中选择一个项目时,它会在activity_main layout的顶部显示选择的布局。我认为transition.replace()
方法不起作用或者它没有用选定的布局替换当前布局。
这是我的代码
public class MainActivity extends FragmentActivity {
ActionBarDrawerToggle icon;
final String[] listContent ={"Account Setup","Fragment Two",
"Fragment Three","Main Page"};
final String[] fragments ={
"com.strawberryapps.accountsetup.AccountSetup",
"com.strawberryapps.backupnotes.BackupNotes",
"com.strawberryapps.notes.AddNote",
"com.strawberryapps.myapp.MainPage"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> ad = new ArrayAdapter<String> (getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, listContent);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView list = (ListView) findViewById(R.id.drawer);
list.setAdapter(ad);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
@Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
android.support.v4.app.FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[position]));
transition.commit();
}
});
}
});
}
这是我的activity_main.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=".MainActivity"
android:background="#FFFFFF"
>
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#454545"
android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>
我用android 4.3开发
答案 0 :(得分:0)
请更改片段布局.xml文件的背景颜色.... 在父布局
中的片段布局文件中使用此代码<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_home"
android:background="#FFF"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
</RelativeLayout>