android - 在没有 TitleBar 或 ActionBar 的情况下使用 Navigation Drawer

标签 android navigation android-relativelayout android-support-library drawer

我正在向我正在开发的这个应用程序添加一个抽屉导航,我已经搜索了互联网;论坛、stackoverflow、android 开发人员文档,但仍然没有找到很好的答案。

我知道可以在不使用这些东西的情况下做到这一点。我想知道的是如何。 NsMenuAdapter模型使用了一个title,然后就有了这些功能

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

这显然是在寻找一个 Action 栏。我尝试了几个没有用的模型,我刚刚完成尝试的大模型位于此处 How to Add icons adjacent to titles for Android Navigation Drawer (这与我下面的链接有关,该项目来自 gitHub here https://github.com/gabrielemariotti/androiddev/tree/master/NavigationDrawer )。现在关键是,我正在使用自定义布局(即相对布局与线性布局混合),我真的不知道下一步应该做什么才能让它发挥作用。

旁注:当我的 main_activity.xml(抽屉导航的实现)中只有 ListView 时,它会像预期的那样正确地滑出。但我终其一生都无法弄清楚如何用数据填充它。我基本上需要 3 个标题,其中包含可点击的导航元素,元素旁边有图标。

我转向这个模型是为了获得关于如何通过相对布局做到这一点的大部分见解 http://gmariotti.blogspot.com/2013/05/creating-navigation-drawer.html但他们使用 Action /标题栏,这真的让我陷入困境。

最佳答案

其实很简单。比使用 ActionBar 更容易。我正在用几乎最简单的布局写答案

让你的 xml 像这样:

<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" >

<!-- This is how your main page will look, just 2 buttons -->

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:onClick="onLeft"
            android:text="left" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="onRight"
            android:text="right" />
    </RelativeLayout>

<!-- Left Drawer -->
    <RelativeLayout
    android:id="@+id/whatYouWantInLeftDrawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start" >

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_dark" />
    <!-- you can have many more widgets here like buttons or labels -->
    </RelativeLayout>


<!-- Right Drawer -->
    <RelativeLayout
    android:id="@+id/whatYouWantInRightDrawer"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right" >

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_light" />
    <!-- you can have many more widgets here like buttons or labels -->
    </RelativeLayout>

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

然后让你的 Activity 像这样:

public class MainActivity extends Activity {

RelativeLayout leftRL;
RelativeLayout rightRL;
DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      I'm removing the ActionBar.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
        rightRL = (RelativeLayout)findViewById(R.id.whatYouWantInRightDrawer);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    }

    public  void onLeft(View view) {
         drawerLayout.openDrawer(leftRL);
    }

    public  void onRight(View view) {
         drawerLayout.openDrawer(rightRL);
    }
}

就是这样。希望对您有所帮助。

关于android - 在没有 TitleBar 或 ActionBar 的情况下使用 Navigation Drawer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17735621/

相关文章:

java - 无法在同一 Activity 中创建多个 YoutubePlayerFragment

css - 强制 Logo 与菜单保持一致

ios - 如何更改默认的 iOS 后退行为以避免在两个 View 之间来回切换?

java - Android Spinner RealtiveLayout 作为项目

android - 寻找附近的地点

java - 使 toast 在互联网连接等待循环之前显示

java - 带有空引用的 Guava/Otto EventBus

android - 带有图标的 ActionBar 微调器(如 Google map ),我该如何实现?

Android Removing View 不结束它的动画

android - RelativeLayout - 无法解析资源