简体   繁体   English

如何使工具栏按钮可点击?

[英]How to make the Toolbar button clickable?

I have a hamburger button in custom toolbar.我在自定义工具栏中有一个汉堡包按钮。 when I click button, I want to open slide menu.当我单击按钮时,我想打开幻灯片菜单。 My project is debug, but when I click the hamburger button, slide menu not open.我的项目是调试的,但是当我点击汉堡按钮时,滑动菜单没有打开。 Any idea?任何想法?

在此处输入图像描述

MainActivity:主要活动:

       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: 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>

try to add Image click listner and show and hide your drawer layout like this尝试添加图像点击列表器并像这样显示和隐藏您的抽屉布局

rightIcon.setOnClickListener {
        if (drawerLayout.isOpen) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            drawerLayout.openDrawer(GravityCompat.START)
        }
    }

An imageview is not clickable by default - try to add "Clickable=true" and perhaps "Focusable=true" as well.默认情况下,imageview 不可点击 - 尝试添加“Clickable=true”,也可以添加“Focusable=true”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM