美文网首页
安卓滑动菜单

安卓滑动菜单

作者: 水固态中 | 来源:发表于2017-11-28 15:55 被阅读0次

DrawerLayout控件

布局xml代码

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
    android:layout_height="match_parent">
//第一个控件是主屏幕显示的内容
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:layout_gravity="center"
  android:text="钣金展开"
  android:textColor="@color/white"
  android:textSize="18sp"/>
</android.support.v7.widget.Toolbar>
 
</FrameLayout>
//第2个子控件是滑动菜单显示的内容
<TextView
android:layout_width="match_parent"
    android:layout_height="match_parent"
android:text="这是滑动菜单"
android:layout_gravity="start"
android:background="#578543"/>
</android.support.v4.widget.DrawerLayout>

现在就可以滑动显示了。

添加滑动导航菜单

添加私有成员变量

private DrawerLayout drawerLayout;

oncreate添加

drawerLayout=(DrawerLayout)findViewById(R.id.drawerLayout);
  ActionBar actionBar=getSupportActionBar();
  if(actionBar!=null){
  //设置ActionBar的向上导航键显示
  actionBar.setDisplayHomeAsUpEnabled(true);
  //修改图标 actionBar.setHomeAsUpIndicator(R.drawable.menu);

  onOptionItemSelected添加

case android.R.id.home:
//打开滑动菜单
drawerLayout.openDrawer(GravityCompat.START);
return true;

ww

相关文章

网友评论

      本文标题:安卓滑动菜单

      本文链接:https://www.haomeiwen.com/subject/xikebxtx.html