Android官方组件DrawerLayout简单使用

Android官方组件DrawerLayout简单使用

一、先写好布局main.xml

	<?xml version="1.0" encoding="utf-8"?>
	<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:id="@+id/main_drawer"
	    tools:context=".MainActivity">
	
	        <LinearLayout
	            android:id="@+id/center_content"
	            android:layout_width="match_parent"
	            android:layout_height="match_parent"
	            android:gravity="center"
	            android:orientation="vertical">
	
	            <TextView
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="主界面"
	                android:textSize="20sp"
	                android:textColor="#ffff00"/>
	
	        </LinearLayout>
	
	        <FrameLayout
	            android:id="@+id/left_content"
	            android:layout_width="240dp"
	            android:layout_height="match_parent"
	            android:layout_gravity="start"
	            android:background="#ffffff">
	
	            <LinearLayout
	                android:layout_width="match_parent"
	                android:layout_height="wrap_content"
	                android:orientation="horizontal">
	                <ImageButton
	                    android:layout_width="wrap_content"
	                    android:layout_height="wrap_content"
	                    android:layout_margin="10dp"
	                    android:id="@+id/image_button_portrait"
	                    android:background="@drawable/ic_person_black_24dp"/>
	
	                <TextView
	                    android:layout_width="wrap_content"
	                    android:layout_height="wrap_content"
	                    android:layout_margin="10dp"
	                    android:text="@string/name"
	                    android:textColor="#0000ff"
	                    android:textSize="20sp"/>
	
	            </LinearLayout>
	
	            <LinearLayout
	                android:layout_width="match_parent"
	                android:layout_height="wrap_content"
	                android:layout_gravity="bottom"
	                android:orientation="horizontal">
	                <ImageButton
	                    android:layout_width="wrap_content"
	                    android:layout_height="wrap_content"
	                    android:layout_margin="10dp"
	                    android:id="@+id/image_button_setting"
	                    android:background="@drawable/ic_settings_black_24dp"/>
	            </LinearLayout>
	
	        </FrameLayout>
	
	</android.support.v4.widget.DrawerLayout>

其中的第一个LinearLayout是主界面,后面的FrameLayout是侧滑栏中的内容(注意:DrawerLayout将第一个布局作为主界面,第二个布局作为侧滑栏的内容)。

注意其中的android:layout_gravity="start"是必须的,它将决定你使用的是从左划出还是从右划出(start代表从左向右划出,end代表从右向左滑出)

接下来就可以使用了,还有其他的功能自行去体会。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值