简单的抽屉效果

来源:互联网 发布:诺基亚n95软件下载 编辑:程序博客网 时间:2024/05/11 15:07


抽屉式导航栏是一个面板,它将应用的主要导航选项显示在屏幕左边缘。大多数情况下,它处于隐藏状态,但是如果用户从屏幕左边缘滑动手指,同时在应用顶层触摸操作栏中的应用图标,它将会显示出来。

要添加抽屉式导航栏,请将包含DrawerLayout对象的用户界面声明为布局的根视图。在DrawerLayout内,添加一个包含屏幕主内容(当抽屉式导航栏处于隐藏状态时为主要布局)的视图和另一个包含抽屉式导航栏内容的视图。

有以下布局特性:

  • DrawerLayout中,主内容视图(上面的FrameLayout必须是第一个子视图,因为 XML 顺序意味着按 z序(层叠顺序)排序,并且抽屉式导航栏必须位于内容顶部。
  • 主内容视图设置为匹配父视图的宽度和高度,因为在抽屉式导航栏处于隐藏状态时,它代表整个 UI。
  • 抽屉式导航栏视图 (ListView)必须使用android:layout_gravity 属性指定其水平重力。要支持“从右到左”(RTL)语言,请使用 "start"(而非 "left")指定该值(这样当布局为RTL 时,抽屉式导航栏会显示在右侧)。
  • 抽屉式导航栏视图以 dp 为单位指定其宽度,且高度与父视图相匹配。抽屉式导航栏的宽度不应超过320dp,从而用户始终可以看到部分主内容。


实现一个简单的抽屉效果,首先要添加第三方jar包(android.support.v4.jar里面有实现类),然后根据DrawerLayout的规则编写以下布局文件即可实现抽屉效果。


<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/drawLayout"
    tools:context=".MainActivity" >

    <!-- 主内容区 -->
    <FrameLayout
        android:id="@+id/ff"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
      >
        <TextView
            android:id="@+id/tv"
            android:text="这是主内容区"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        
    </FrameLayout>
 
    <!-- 抽屉导航栏区域 -->
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical"
        android:background="@android:color/white"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher"
            />
        
        <ListView
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            >
        </ListView>
    </LinearLayout>
    

</android.support.v4.widget.DrawerLayout>



更多信息:https://developer.android.com/training/implementing-navigation/nav-drawer.html#Init

0 0