实现导航抽屉的教程

时间:2015-02-20 20:59:57

标签: android navigation-drawer

我拼命想在我的小型Android项目中实现一个简单的导航抽屉。我希望它的行为与您对抽屉的期望一样,并且当您点击一个项目时开始新的活动。

但我无法让它发挥作用。我一步一步地遵循了无数的教程,但我陷入了困境。

那么,是否有一个简单的直接教程,只需添加一个带有列表的抽屉,该列表可以启动点击活动?我的意思是它不能用来编写成百上千行的代码来让这种“简单”的东西起作用。 我不需要任何花哨的图标或其他任何东西!像this这样的小事会让我开心。

编辑:有人向我推荐了this视频。这真的很有帮助。

1 个答案:

答案 0 :(得分:1)

我发现这个教程效果非常好并且编码很好。

Tute Central Tutorial Navigation Drawer

对于导航抽屉,您必须使用FragmentActivity,并且每次点击抽屉物品都会更改活动上的片段。

可能会出现一些错误

  1. 有时单击抽屉项目不起作用。这是由于布局格式不佳造成的。你必须使用这种布局才能从android中获得最多 -

    <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">
    
    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->
    
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#ffff"/>
    

  2. 如果您想要插入更多观看次数 - 对于要插入的其他布局,您可以将<FrameLayout>替换为<RelativeLayout>并在其中放置框架布局。例如 -

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" /> 
    
        <!-- Any other view here -->
    </RelativeLayout>
    
  3. 如果您需要任何其他帮助!让我知道。快乐的编码

    干杯

相关问题