【问题标题】:How to create a custom navigation drawer in android?如何在android中创建自定义导航抽屉?
【发布时间】:2014-04-16 08:44:52
【问题描述】:

嗨,我正在做一个 android 项目,我想在所有页面中显示导航抽屉。在主屏幕中,想要在左侧顶部显示导航抽屉,在屏幕右上角的圆圈内显示登录该应用的用户的图片。

如果有人知道使用导航抽屉创建自定义操作栏,请帮助我。

【问题讨论】:

    标签: android android-actionbar navigation-drawer


    【解决方案1】:

    您需要在菜单文件夹中为右侧的图像添加一个菜单文件。

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item
        android:id="@+id/your_id"
        android:icon="@drawable/the image you want"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="Your title"/>
    </menu>
    

    然后在你想把这段代码放进去的activity中(onCreate之外):

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
    
        return true;
    }
    

    这会将菜单放在顶部。

    现在对于右侧的图像(三行),将其放入活动的 xml 中(这也会在侧面添加抽屉,您可以像任何列表视图一样自定义它)

    <android.support.v4.widget.DrawerLayout      xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstActivity" >
    
    <!-- The main content goes here -->
    
     <!-- The navigation drawer -->
    
    <ListView  
        android:id="@+id/drawer1"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#FFFFFF"
        android:choiceMode="singleChoice"
        android:divider="#FF0000"
        android:dividerHeight="1dp"
        android:paddingLeft="15sp"
        android:paddingRight="15sp" />
    
    </android.support.v4.widget.DrawerLayout>
    

    然后把这部分放到你的 java.lang.您需要从某个地方获取 ic-drawer 图像,但到处都是,所以只需 google 即可。

        final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout1);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
        drawer, /* DrawerLayout object */
        R.drawable.ic_drawer, /* This is the image for top left*/
        R.string.drawer_open, /* "open drawer" description */
        R.string.drawer_close /* "close drawer" description */
        );
    

    【讨论】:

    • 非常感谢。我会试试你说的方法。
    • 没问题,我上周为一个应用程序做了这个,这个教程也有很大帮助。 tutecentral.com/android-custom-navigation-drawer
    • 我看过那个教程。但还没有解决方案。如果你有代码可以给我sn-p吗..
    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2014-07-12
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多