首先,介绍一下DrawerLayout:DrawerLayout事实上就是一个控件,使用起来十分方便 ,它就相当于是一个自带侧滑功能的LinearLayout,在你设定完宽高后可直接在里面放置控件或布局,当然,里面的点击以及各种监听或事件都需使用者自己实现,自主性很高,下面来看一下一个简单实现侧滑的示例图:
以上是为侧滑菜单的演示图(此示例只能从左边最边缘呼出侧边菜单,不能全屏侧滑呼出,若需要全屏侧滑呼出,则需自己设置属性),下面给大家上实现代码:
activity_main.xml部分(复制以后别忘了把穿插在代码中的注释删掉):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.test_0821.MainActivity">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主内容区的布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
<!--如果主内容区内容复杂,建议与Fragment配合使用-->
</android.support.v4.view.ViewPager>
</LinearLayout>
<!--侧滑菜单布局-->
<LinearLayout
android:layout_width="210dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical"
android:choiceMode="singleChoice"
android:background="#292929"
>
<!--头像放置处-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="horizontal"
android:gravity="center"
>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/y1"<!--演示图中头像-->
android:layout_marginRight="8dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="谦之君子"
android:textColor="#e0e0e0"
android:textSize="22sp"
/>
</LinearLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!--博客-->
<RadioButton
android:background="@drawable/ce_selector"
android:drawableLeft="@drawable/office1"<!--文字前的小图标-->
android:layout_width="match_parent"
android:layout_height="45dp"
android:button="@null"
android:text=" 博 客"
android:textColor="#e0e0e0"
android:textSize="22sp"
android:paddingLeft="18dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#666666"
/>
<!--发现-->
<RadioButton
android:background="@drawable/ce_selector"
android:drawableLeft="@drawable/form1"<!--文字前的小图标-->
android:layout_width="match_parent"
android:layout_height="45dp"
android:button="@null"
android:text=" 发 现"
android:textSize="22sp"
android:textColor="#e0e0e0"
android:paddingLeft="18dp"
/>
<!--我的-->
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#666666"
/>
<RadioButton
android:background="@drawable/ce_selector"
android:drawableLeft="@drawable/person2"<!--文字前的小图标-->
android:layout_width="match_parent"
android:layout_height="45dp"
android:button="@null"
android:text=" 我 的"
android:textColor="#e0e0e0"
android:textSize="22sp"
android:paddingLeft="18dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#666666"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="210dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="退出登录"
android:textColor="#ffffff"
android:textSize="18sp"
android:gravity="center"
android:onClick="exit_all"
android:background="@drawable/cehua_shape"<!--外部样式-->
/>
</LinearLayout>
</RadioGroup>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
MainActivity.java部分:
package com.example.test_0821;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
外部样式:ce_selector.xml部分:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/shape_main_cegroup"></item>
<item android:state_checked="false" android:drawable="@drawable/shape_main_cegroup2"></item>
</selector>
cehua_shape.xml部分:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:color="#fefbfb"
android:width="1dp"
></stroke>
<corners
android:radius="3dp"
></corners>
</shape>
shape_main_cegroup.xml部分:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#cd0000"
></solid>
</shape>
shape_main_cegroup2.xml部分:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>
最后,如果需要用到控件的点击事件或监听实现功能的话,需要在相应的控件上加id自行实现功能~