美文网首页Android实战
Android DrawerLayout 使用

Android DrawerLayout 使用

作者: 基本密码宋 | 来源:发表于2017-05-23 11:31 被阅读120次
    drawable.gif

    闲着没事儿 做个小东西
    附上代码

    主布局
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <include
            layout="@layout/layout_toolbar"
            />
    
        <include
            layout="@layout/layout_drawer"
            />
    </LinearLayout>
    
    inclue包含的布局
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tl_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
    </android.support.v7.widget.Toolbar>
    
    android:popupTheme   从toolbar弹框的样式   
    app:theme  toolbar主题    此属性可以添加到任意的view上
    --------------------------------------------------------------------------------------------------------------------
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/left"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#fff">
    
            <ListView
                android:id="@+id/lv_listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@null"
                android:text="DrawerLayout">
    
            </ListView>
        </LinearLayout>
    
    </android.support.v4.widget.DrawerLayout>
    
     android:layout_gravity="start"    标志哪个是侧滑的布局
    

    下面贴出 activity中的代码

    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    import handongkeji.com.db.R;
    
    /**
     * 进行书写 数据库 greenDao
     */
    public class MainActivity extends AppCompatActivity {
    
        private Toolbar toobalTitle;
        private ListView lvListView;
        private DrawerLayout drawableLayout;
        private ActionBarDrawerToggle mDrawerToggle;
        private String[] lvs = {"List Item 01", "List Item 02", "List Item 03", "List Item 04"};
        private ArrayAdapter arrayAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toobalTitle = (Toolbar) findViewById(R.id.tl_custom);
            lvListView = (ListView) findViewById(R.id.lv_listView);
            drawableLayout = (DrawerLayout) findViewById(R.id.left);
            toobalTitle.setTitle("Toolbar");//设置Toolbar标题
            toobalTitle.setTitleTextColor(Color.parseColor("#ffffff")); //设置标题颜色
            setSupportActionBar(toobalTitle);
            getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            //创建返回键,并实现打开关/闭监听
            mDrawerToggle = new ActionBarDrawerToggle(this, drawableLayout, toobalTitle, R.string.open, R.string.close) {
                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);            Toast.makeText(MainActivity.this,"开",Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
             Toast.makeText(MainActivity.this,"关",Toast.LENGTH_SHORT).show();
                }
            };
            mDrawerToggle.syncState();
            drawableLayout.setDrawerListener(mDrawerToggle);
            //设置菜单列表
            arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, lvs);
            lvListView.setAdapter(arrayAdapter);
        }
    }
    
    

    当然还有最主要的代码
    style

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="colorPrimaryDark">@color/Indigo_colorPrimaryDark</item>
            <!--Toolbar颜色-->
            <item name="colorPrimary">@color/Indigo_colorPrimary</item>
            <!--返回键样式-->
            <item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
        </style>
    
        <style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
            <item name="color">@android:color/white</item>
        </style>
    
        <color name="Indigo_colorPrimaryDark">#303f9f</color>
        <color name="Indigo_colorPrimary">#3f51b5</color>
        <color name="Indigo_nav_color">#4675FF</color>
    
    </resources>
    
    

    相关文章

      网友评论

        本文标题:Android DrawerLayout 使用

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