大家好,
我是Android编程的新手,目前正在开发一个或多或少是教程代码拼凑的练习应用程序。
现在我的工具栏下方有导航抽屉。我想重新调整xml层次结构,使其与Material Design指南保持一致,并在工具栏上方有导航抽屉。这似乎是一项简单的任务,但对于我的生活,我似乎无法得到它。
有人可以提出任何建议吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:apps="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/background_color">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary_color"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
apps:title="@string/app_name"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:apps="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:apps="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
apps:itemTextColor="#000"
android:fitsSystemWindows="true"
apps:headerLayout="@layout/navigation_header"
apps:menu="@menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
package android.dohyun.projectannie;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.frame_layout, new NotesFragment()).commit();
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if(menuItem.getItemId() == R.id.menu_notes) {
FragmentTransaction fragmentTransactionNotes = mFragmentManager.beginTransaction();
fragmentTransactionNotes.replace(R.id.frame_layout, new NotesFragment()).commit();
}
if(menuItem.getItemId() == R.id.menu_folders) {
FragmentTransaction fragmentTransactionFolders = mFragmentManager.beginTransaction();
fragmentTransactionFolders.replace(R.id.frame_layout, new FoldersFragment()).commit();
}
if(menuItem.getItemId() == R.id.menu_trash) {
FragmentTransaction fragmentTransactionTrash = mFragmentManager.beginTransaction();
fragmentTransactionTrash.replace(R.id.frame_layout, new TrashFragment()).commit();
}
if(menuItem.getItemId() == R.id.menu_settings) {
FragmentTransaction fragmentTransactionSettings = mFragmentManager.beginTransaction();
fragmentTransactionSettings.replace(R.id.frame_layout, new SettingsFragment()).commit();
}
if(menuItem.getItemId() == R.id.menu_info) {
FragmentTransaction fragmentTransactionInfo = mFragmentManager.beginTransaction();
fragmentTransactionInfo.replace(R.id.frame_layout, new InfoFragment()).commit();
}
return false;
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
上面的代码是Ratan编写的一个稍微修改过的版本: https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library/
答案 0 :(得分:0)
DrawerLayout
是一个支持导航抽屉的特殊FrameLayout
。
主视图中没有layout_gravity
的子视图。具有layout_gravity
的子视图,例如android:layout_gravity="start"
是导航抽屉。
这意味着DrawerLayout
应该是顶级视图组。换句话说,将Toolbar
放在<{1}}内的<{1>}。