美文网首页
MaterialDesign的Toolbar以及Drawerla

MaterialDesign的Toolbar以及Drawerla

作者: 2f479b1467f1 | 来源:发表于2017-07-04 14:19 被阅读18次
image.png

类似于上图的效果,点击顶部左侧的按钮会出现菜单栏(类似于SlideMenu),右侧的搜索按钮也会出现相应的事件。

我们只需要两个MD的组件就可以完成:

一,Drawerlayout

布局如下:

<android.support.v4.widget.DrawerLayout   
    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:context=".MainActivity">

    <include
        android:id="@+id/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="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPageBg"
        android:clickable="true"
        android:fitsSystemWindows="true" />

   
</android.support.v4.widget.DrawerLayout>

第一个Include是页面主页面,只需要将他的width和hight设置成matchparent,剩余的布局部分就是菜单的布局了。

二、toolbar
为toolbar设置布局可以直接在XML内他的标签内进行布局类似于:

  <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorTheme"
        app:contentInsetStart="0.0dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ToolbarStyle">

        <FrameLayout
            android:id="@+id/ll_title_menu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:paddingLeft="15dp"
            android:paddingRight="15dp">

            <ImageView
                android:id="@+id/iv_title_menu"
                android:layout_width="23dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/titlebar_menu" />
        </FrameLayout>

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="?attr/selectableItemBackgroundBorderless"
                app:theme="@style/AppTheme">

                <ImageView
                    android:id="@+id/iv_title_gank"
                    android:layout_width="55dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@drawable/titlebar_disco" />

                <ImageView
                    android:id="@+id/iv_title_one"
                    android:layout_width="55dp"
                    android:layout_height="match_parent"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@drawable/titlebar_music" />

                <ImageView
                    android:id="@+id/iv_title_dou"
                    android:layout_width="55dp"
                    android:layout_height="match_parent"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@drawable/titlebar_friends" />

            </LinearLayout>
        </HorizontalScrollView>
    </android.support.v7.widget.Toolbar>

这种写法吧左侧的菜单按钮放在布局中 作为一Imageview来进行操作,还有一种方法是直接在代码中设置:

   if (toolbar != null) {
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_ab_drawer);
   }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawerLayout.openDrawer(Gravity.START);

        case R.id.action_search:
            Toast.makeText(SampleActivity.this, "search", Toast.LENGTH_LONG).show();
            return  true;
    }

    return super.onOptionsItemSelected(item);
}

注: setSupportActionBar(toolbar);方法一定要在其他设置toolbar的方法执行!
同样的道理,右侧的搜索按钮也可以设置在代码中,首先需要创建一个res/menu目录下创建一个menu文件:

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_launcher"
android:orderInCategory="100"
android:title="search"
app:showAsAction="always" />
</menu>

重写方法

@Override
public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.main, menu);
         return true;
    }

相关文章

网友评论

      本文标题:MaterialDesign的Toolbar以及Drawerla

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