Design Support Library 提供了NavigationView 来帮助我们实
现抽屉菜单界面,和普通的侧拉菜单实现方式一样,所有的东西还是都放在一个DrawerLayout 中,用 NavigationView 来替代我们此前自定义的控件。
使用
xml 布局代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:id="@+id/drawer_layout">
<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:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/draw_view">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
属性 | 作用 |
---|---|
android:layout_gravity | 设置侧滑从哪边划出(start,left,right) |
app:headerLayout | 设置头布局文件 |
app:menu | menu 点击项 |
res/layout/nav_header.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="150dp"
android:background="?attr/colorPrimary"
android:orientation="horizontal"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:background="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="yuzhiyi"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="20sp"/>
</LinearLayout>
res/layout/draw_view.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@mipmap/img00"
android:title="Home" />
<item
android:id="@+id/nav_message"
android:icon="@mipmap/img05"
android:title="Message" />
<item
android:id="@+id/nav_friends"
android:icon="@mipmap/img10"
android:title="Friends" />
<item
android:id="@+id/nav_disuccion"
android:icon="@mipmap/img15"
android:title="Disuccion" />
</group>
<item android:title="Sub items">
<menu>
<item
android:id="@+id/nav_sub_items_1"
android:icon="@mipmap/img20"
android:title="Sub items 1" />
<item
android:id="@+id/nav_sub_items_2"
android:icon="@mipmap/img24"
android:title="Sub items 2" />
</menu>
</item>
</menu>
java 代码
public class NavigationViewActivity extends AppCompatActivity{
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_navifationview);
mDrawerLayout = findViewById(R.id.drawer_layout);
mNavigationView = findViewById(R.id.nav_view);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
item.setChecked(true);
String title = item.getTitle().toString();
Toast.makeText(NavigationViewActivity.this, title, Toast.LENGTH_SHORT).show();
mDrawerLayout.closeDrawers();
return true;
}
});
}
}