DrawerLayout+FragmentTabHost
本节主要内容:
- DrawerLayout简介
- DrawerLayout使用
- FragmentTabHost简介
- 进行项目编码
1.DrawerLayout简介
DrawerLayout这个类位于android.support.v4.widget包下,要想在项目中使用它,必须在gradle文件中添加依赖,格式如下:
compile 'com.android.support:support-v4:23.0.1'
另外我在项目中使用了ButterKnife,有关于ButterKnife的详细信息参见官网ButterKnife官网,在项目中通过下面这句添加ButterKnife依赖:
compile 'com.jakewharton:butterknife:7.0.1'
由于项目后面会用到动画效果所以还要添加nineoldandroids依赖:
compile 'com.nineoldandroids:library:2.4.0'
完整的dependencies如下所示:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.nineoldandroids:library:2.4.0'
}
2.DrawerLayout使用
在代码中通过引用com.android.support.v4.widget.DrawerLayout创建DrawerLayout,完整的实例如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/activity_main_menu_drawerlayout">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/activity_main_open_right_menu"
android:layout_alignParentRight="true"
android:text="右边菜单"
android:layout_marginRight="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
<fragment
android:id="@+id/activity_main_left_menu"
android:name="类的完整名称"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:tag="LEFT">
</fragment>
<fragment
android:id="@+id/activity_main_right_menu"
android:name="类的完整名称"
android:layout_width="20dp"
android:layout_gravity="right"
android:layout_height="fill_parent"
android:tag="RIGHT"></fragment