如何在安卓系统中开始使用导航抽屉



我对Android并不完全陌生,也了解最基本的东西。但我仍然对导航抽屉之类的模板活动有一些问题。

目前了解这方面的最佳方式是什么?有很多新功能,大多数教程都是相当旧的

非常感谢

这里我将逐步解释如何实现导航抽屉。无论你有什么疑问,只要在谷歌上搜索一下。我在评论中解释了大部分内容。

步骤:1(在xml布局中创建DrawerLayout,比如activity_main.xml,并在其中添加导航视图。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true" <!--it will fit it in full system window-->
tools:openDrawer="start" <!--it means that the drawer open from left side-->
android:id="@+id/drawer_layout">
<include
android:id="@+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/app_bar_main"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"  <!-- it specify where you want to show icon -->
app:headerLayout="@layout/nav_header_main" <!-- it include header-->
app:menu="@menu/activity_main_drawer_menu" /> <!-- it inlude menu means the items below the header-->
</androidx.drawerlayout.widget.DrawerLayout>

步骤:2(为工具栏创建app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

步骤:3(创建content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

步骤:4(为标头创建nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="176dp" <!-- you set the height of header-->
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@color/light_blue">
<!--add here whatever you want to show in header of navigation drawer-->

</LinearLayout>

步骤5:(为标题下方的菜单项创建activity_main_drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home" />
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_info_black_24dp"
android:title="About"/>
</group>
</menu>

步骤:6(最终创建MainActivity.java

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
Fragment fragment;
FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer=findViewById(R.id.drawer_layout);
NavigationView navigationView=findViewById(R.id.nav_view);
View headerView=navigationView.getHeaderView(0); //here you can take view of header

ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
fragment=new yourFragment();  //here you add which fragment you want to display at the starting
ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,fragment);
ft.commit();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id=item.getItemId();
if (id==R.id.nav_home) {
fragment=new yourFragment();
}
else if (id==R.id.nav_about) {
fragment=new yourFragment();
}

ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,fragment);
ft.commit();
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}
@Override
protected void onPause() {
drawer.closeDrawer(GravityCompat.START);
super.onPause();
}

最新更新