gpt4 book ai didi

android - 如何在抽屉导航的右上角添加十字按钮

转载 作者:行者123 更新时间:2023-11-30 01:02:36 26 4
gpt4 key购买 nike

我创建了一个抽屉导航,当用户点击 汉堡包 图标时,它会打开抽屉。我需要在 drawer 中添加一个关闭按钮。这就是我需要实现的。

enter image description here

我已经尝试过,但是无法在抽屉布局 上添加图像。这是我添加 Navigation Drawer 的代码。请指导我如何在抽屉的右上角添加图像。

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/base_layout">

<include layout="@layout/header_layout"/>


</LinearLayout>


<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="start"
android:background="@drawable/sidebg"
app:menu="@menu/drawer_menu"
app:itemTextColor= "#ffffff"
app:headerLayout="@layout/navigation_drawer_header"
android:gravity="bottom|left"
android:dividerHeight="0dp"
app:itemIconTint="@android:color/white"
>

</android.support.design.widget.NavigationView>




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

最佳答案

ActionBarDrawerToggle将解决您的问题:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};

// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);

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

替代方法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<!--your layout here -->
</android.support.v4.widget.DrawerLayout>


<ImageView
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:id="@+id/im_close_btn"
android:layout_width="48dp"
android:layout_height="48dp"
android:visibility="gone"
android:src="@drawable/ic_close"/>
</RelativeLayout>

关于android - 如何在抽屉导航的右上角添加十字按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39235107/

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