android - 如何使工具栏按钮可点击?
问题描述
我在自定义工具栏中有一个汉堡包按钮。当我单击按钮时,我想打开幻灯片菜单。我的项目正在调试,但是当我单击汉堡按钮时,滑动菜单未打开。任何想法?
主要活动:
class MainActivity : AppCompatActivity() {
private lateinit var userPresenter: UserPresenterInterface
private var drawerLayout: DrawerLayout? = null
private var toggle: ActionBarDrawerToggle? = null
private var rightIcon: Toolbar? = null
private var navigationView: NavigationView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
userPresenter = UserPresenter(resources)
supportActionBar?.elevation = 0f
rightIcon?.setOnClickListener {
drawerLayout = findViewById(R.id.drawer)
rightIcon = findViewById(R.id.right_icon)
setSupportActionBar(rightIcon)
toggle = ActionBarDrawerToggle(this, drawerLayout, rightIcon,
R.string.open, R.string.close)
drawerLayout!!.addDrawerListener(toggle!!)
toggle!!.syncState()
navigationView = findViewById(R.id.nav_view)
}
custom_toolbar.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:id="@+id/right_icon"
android:layout_centerVertical="true"
android:src="@drawable/ic_baseline_menu_24"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
解决方案
尝试添加图像点击列表器并像这样显示和隐藏您的抽屉布局
rightIcon.setOnClickListener {
if (drawerLayout.isOpen) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
drawerLayout.openDrawer(GravityCompat.START)
}
}
推荐阅读
- migration - 迁移到 .net core 3.1 后,授权发生在身份验证之前
- c++ - 对在类外声明的函数的未定义引用
- php - 带有 csrf 的 Laravel ajax 请求
- javascript - 我如何像冠状病毒一样设置 OpenStreetMap 的样式
- php - 致命错误:未捕获错误:调用未定义函数 redirect() PHP 注销会话
- amazon-web-services - 将 cloudformation 无服务器模板转换为 Terraform 的最佳方法
- typescript - 元素隐式具有“任何”类型,因为类型的表达式
- reactjs - 我应该同时使用 Facebook Relay 和 Redux
- python - Django中的购物车总价问题
- angular - Spring Boot 后端和 Angular 前端应用程序