微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 使用没有TitleBar或ActionBar的导航栏

我正在为这个应用程序添加一个导航栏,我正在开发,我已经浏览了互联网;论坛,stackoverflow,android开发人员文档,还没有找到一个很好的答案.

我知道有可能这样做,而不使用这些东西.我想知道是怎么回事NsMenuAdapter模型使用一个标题,然后有这些功能

getActionBar().setdisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

其中显然是寻找一个动作栏.我尝试了几个没有工作的模型,我刚刚完成的大型模块位于这里How to Add icons adjacent to titles for Android Navigation Drawer(这与我以下的链接相关,该项目来自gitHub这里是https://github.com/gabrielemariotti/androiddev/tree/master/NavigationDrawer).现在的关键是,我正在使用一个自定义的布局(即相对布局与线性布局混合),我真的很失去了我的下一步应该是为了让这个工作.

Sidenote:当我的main_activity.xml(导航抽屉的实现)中只有ListView时,它正好像想象的那样滑出来.但是我不知道如何用数据填充它.我基本上需要3个标题,它们将具有可点击的导航元素,其中包含元素旁边的图标.

我转向这个模型,大部分我的见解如何通过相对布局http://gmariotti.blogspot.com/2013/05/creating-navigation-drawer.html这样做但是他们使用动作/标题栏,这是真正的扔给我一个循环.

解决方法

实际上很简单比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>

然后让你的活动如下所示:

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);
    }
}

而已.希望有帮助

原文地址:https://www.jb51.cc/android/311122.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐