好的,所以我对android开发还很陌生,我正在为我的老板开发一个项目,要求应用程序有一个导航抽屉。问题是所有页面都显示相同的内容(只应该在第一页上的内容)。我的问题是如何让所有页面都有不同的内容,我在哪里放这些内容?以下是来自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="com.google.auditreport.MainActivity" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead.
-->
<!--
The drawer is given a fixed width in dp and extends the full height of
the container.
-->
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- text view for title -->
<TextView
android:id="@+id/AuditTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal"
android:text="@string/Title" />
<TextView
android:id="@+id/txtLoc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_alignLeft="@+id/ReportDateTV"
android:text="@string/Location"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal|end" />
<!-- edit text for report scope -->
<EditText
android:id="@+id/LocET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtLoc"
android:layout_toRightOf="@+id/txtLoc"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<!-- text view for audit date field -->
<TextView
android:id="@+id/AuditDatesTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtLoc"
android:layout_marginTop="120dp"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal"
android:text="@string/AuditDates" />
<!-- edit text for audit dates -->
<EditText
android:id="@+id/AuditDateET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/AuditDatesTV"
android:layout_alignLeft="@+id/AuditTV"
android:ems="10"
android:inputType="date" />
<!-- edit text for report scope -->
<EditText
android:id="@+id/ScopeET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ScopeTV"
android:layout_toRightOf="@+id/ScopeTV"
android:ems="10"
android:inputType="text" />
<!-- text view for date of the report -->
<TextView
android:id="@+id/ReportDateTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ScopeTV"
android:layout_below="@+id/AuditDatesTV"
android:layout_marginTop="40dp"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal"
android:text="@string/ReportDate" />
<!-- text view for scope -->
<TextView
android:id="@+id/ScopeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ReportDateTV"
android:layout_marginTop="40dp"
android:layout_toLeftOf="@+id/AuditDateET"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal"
android:text="@string/Scope" />
<!-- text view for exclusions -->
<TextView
android:id="@+id/ExclusionsTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ScopeTV"
android:layout_below="@+id/ScopeTV"
android:layout_marginTop="40dp"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal"
android:text="@string/Exclusions" />
<!-- edit text for exclusions -->
<EditText
android:id="@+id/ExclusionsET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ExclusionsTV"
android:layout_alignLeft="@+id/ScopeET"
android:ems="10"
android:inputType="text" />
<!-- text view for standard -->
<TextView
android:id="@+id/StandardTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ExclusionsTV"
android:layout_below="@+id/ExclusionsTV"
android:layout_marginTop="40dp"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal"
android:text="@string/Standard" />
<!-- edit text for standard -->
<EditText
android:id="@+id/StandardET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/StandardTV"
android:layout_alignLeft="@+id/ExclusionsET"
android:ems="10"
android:inputType="text" />
<!-- text view for preparer -->
<TextView
android:id="@+id/PrepareTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/StandardTV"
android:layout_below="@+id/StandardTV"
android:layout_marginTop="40dp"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal"
android:text="@string/Prepare" />
<!-- edit text for preparer -->
<EditText
android:id="@+id/PrepareET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/PrepareTV"
android:layout_alignLeft="@+id/StandardET"
android:ems="10"
android:inputType="text" />
<!-- text veiw for serial -->
<TextView
android:id="@+id/SerialTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/PrepareTV"
android:layout_below="@+id/PrepareTV"
android:layout_marginTop="40dp"
android:ems="10"
android:gravity="left|center_vertical|center_horizontal"
android:text="@string/ReportSerial" />
<!-- edit text for serial -->
<EditText
android:id="@+id/SerialET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/SerialTV"
android:layout_alignLeft="@+id/PrepareET"
android:ems="10"
android:inputType="text"
android:text="@string/SerialNum" />
<!-- button to poulate the serial number -->
<Button
android:id="@+id/btnSerial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/SerialET"
android:layout_alignRight="@+id/SerialET"
android:layout_below="@+id/SerialET"
android:layout_marginTop="28dp"
android:gravity="left|center_vertical|center_horizontal"
android:onClick="onClick"
android:text="@string/GetSerial" />
<!-- text view for "today's" date -->
<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ScopeTV"
android:layout_alignLeft="@+id/AuditDateET"
android:layout_alignRight="@+id/AuditDateET"
android:textSize="20sp" />
</RelativeLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="com.google.auditreport.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:1)
首先熟悉这里的示例应用http://developer.android.com/training/implementing-navigation/nav-drawer.html。
然后,为了帮助您,朝着正确的方向前进,XML不适合查看。在那里寻找类似BaseActivity.java的东西,你会发现像navDrawerItems.add(new NavDrawerItem...
这样的想法,其中添加了所有菜单选项的列表。你应该能够找到像switch
这样的东西运行这些项目以定义片段,然后使用FragmentManager
来更改正在显示的片段。
以下是我的应用中的一个示例:
/**
* Diplaying fragment view for selected nav drawer list item
* */
public void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ProfileFragment();
break;
case 1:
fragment = new ChartingFragment();
break;
case 2:
fragment = new DayHistoryFragment();
break;
case 3:
fragment = new WeekFragment();
break;
case 4:
fragment = new TemperatureFragment();
break;
case 5:
fragment = new CalendarFragment();
break;
case 6:
fragment = new WhatsHotFragment();
break;
case 7:
fragment = new SettingsFragment();
break;
case 8:
fragment = new DonateFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}