由于我对Android很陌生,我对在Android中使用布局的标准方式感到困惑。基本上,我是iOS开发人员。我用NavigationDrawerView模板创建了一个新项目。现在我必须使用带有导航视图的工具栏和抽屉创建另一个活动。基本上,我想设计一个具有工具栏,抽屉和导航视图的布局,它可以包含在活动中,并且可以在新创建的活动中设计内容。任何人都可以建议最好的方法来做到这一点。我希望每个人都知道导航抽屉视图的默认模板设计。所以我不在这里包含代码。如果有人想查看代码,请告诉我。
答案 0 :(得分:1)
为此你必须使用以下xml文件: content_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.android.MainActivity"//you main activity
tools:showIn="@layout/app_bar_main">
</RelativeLayout>
app_bar_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/actionbar"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</RelativeLayout>
activity_main:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/navDrawerbg"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
制作主要的java文件,即mainactivity extends AppcompatActivity and implements NavigationView.OnNavigationItemSelectedListener
声明全局变量:
private Context mContext;
private static FragmentManager mManager;
Fragment fragment = null;
在oncreate之后创建以下方法
private void initUI(){
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_main, fragment).commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
启动片段视图
private void initiateFragmentView() {
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_main, fragment).commit();
}
}
在setcontentview()
之后在oncreate方法中调用上面的方法 mManager = getSupportFragmentManager();
fragment = new Home();
mContext = this;
initUI();
选择抽屉项目时调用您的片段
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
//Your fragment
fragment = new Home();
initiateFragmentView();
} else if (id == R.id.nav_xyz) {
fragment = new xyz();
initiateFragmentView();
} else if (id == R.id.nav_abc) {
fragment = new abc();
initiateFragmentView();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}