gpt4 book ai didi

android - 如何在android中创建自定义抽屉导航?

转载 作者:行者123 更新时间:2023-11-30 02:57:04 24 4
gpt4 key购买 nike

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

enter image description here

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

最佳答案

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

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

这会将菜单置于顶部。

现在,对于右侧的图像(三行),将其放入 Activity 的 xml 中(这还会在侧面添加抽屉,您可以像自定义任何 ListView 一样对其进行自定义)

<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中。你需要从某个地方获取 ic-drawer 图像,但到处都是,所以只需谷歌搜索即可。

    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 */
);

关于android - 如何在android中创建自定义抽屉导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23104285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com