由于我刚接触Android,因此对于使用Android中的布局的标准方法有些困惑。基本上,我是iOS开发人员。我用NavigationDrawerView模板创建了一个新项目。现在,我必须使用导航视图的工具栏和抽屉创建另一个活动。基本上,我想设计一个具有工具栏,抽屉和导航视图的布局,可以仅将其包含在活动中,而可以在新创建的活动中设计内容。谁能建议最好的方法来做到这一点。我希望每个人都知道导航抽屉视图的默认模板设计。因此,我不在此处包含代码。如果有人想看代码,请告诉我。
为此,您必须使用以下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);
}
启动fragmentview
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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句