簡體   English   中英

如何使工具欄按鈕可點擊?

[英]How to make the Toolbar button clickable?

我在自定義工具欄中有一個漢堡包按鈕。 當我單擊按鈕時,我想打開幻燈片菜單。 我的項目是調試的,但是當我點擊漢堡按鈕時,滑動菜單沒有打開。 任何想法?

在此處輸入圖像描述

主要活動:

       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)
        }
    }

默認情況下,imageview 不可點擊 - 嘗試添加“Clickable=true”,也可以添加“Focusable=true”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM