利用ToolBar和Drawerlayout实现简单的侧滑菜单

activity_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="match_parent"
    android:orientation="vertical">

    <!--toolBar-->
    <include layout="@layout/custom_toolbar"></include>
    <!--DrawerLayout-->
    <include layout="@layout/custom_layout"></include>
</LinearLayout>

custom_layout文件


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--主布局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>
    <!--侧滑布局-->
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:background="#ccc"
        android:layout_gravity="left">
        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null">
        </ListView>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>



custom_toolbar文件

<?xml version="1.0" encoding="utf-8"?>
<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="#0000aa">

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


主函数中

package com.yztc.toolbar02;

import android.graphics.Color;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ListView lv;
    private String[] lvs={"我的订单","我的收藏","配置管理","设置","关于我们"};
    private ArrayAdapter<String> adapter;
    private ActionBarDrawerToggle toggle;
    private ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        mToolbar.setTitle("ToolBar");//设置标题
        mToolbar.setTitleTextColor(Color.RED);//设置标题的颜色
        setSupportActionBar(mToolbar);

        // 设置向上导航的图标可用
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        /**
         * 创建实现抽屉的打开和关闭  mDrawerLayout布局中的抽屉与mToolbar中的向上导航关联
         * ActionBarDrawerToggle DrawerLayout.DrawerListener实现
         */
        toggle=new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,
                R.string.open,R.string.close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                Log.i("tag","-----onDrawerOpened---");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
		
 		super.onDrawerClosed(drawerView);
} @Override public void onDrawerStateChanged(int newState) { super.onDrawerStateChanged(newState); } }; toggle.syncState(); mDrawerLayout.setDrawerListener(toggle); //适配抽屉中listview数据 adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lvs); lv.setAdapter(adapter); } //初始化控件的方法 public void initView(){ mToolbar= (Toolbar) findViewById(R.id.toolbar); mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer); lv= (ListView) findViewById(R.id.lv); iv= (ImageView) findViewById(R.id.iv); }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值