是否可以使背景图像中的图标可点击?

问题描述 投票:0回答:1

我对应用程序开发以及 Android Studio 和布局的使用相对较新。我正在设计我的应用程序并遇到了问题。我有一个 ListView 的 ListItem 设计,其中应显示玩家的姓名和 2 个图标(编辑和删除)。因此,当用户添加玩家时,条目应该像这样显示。

我现在的问题是:如何最好地实现XML中的设计以实现所需的功能?

ListItem Design, created with Figma

我认为可以将整个事情设计为 XML 中的一个按钮,然后该按钮接收此设计作为背景。但问题来了,如何才能让图标可点击呢?

或者有没有更好的方法来做到这一点,我可能会错过这里?

kotlin android-layout background icons clickable
1个回答
0
投票

首先 - 不要使用

ListView
- 它有很多问题。使用
RecyclerView

有多种方法可以做到这一点。您可以使用这个,例如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/text_end"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/text_end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.6"/>

    <ImageView
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="your drawable path goes here"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/text_end"/>

    <ImageView
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="your drawable path goes here"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/edit" />

</androidx.constraintlayout.widget.ConstraintLayout>

不要忘记在

ImageView
src
属性中添加图像路径。

然后在 ViewHolder 中,您可以找到并在每个

onClickListener
 上设置 
ImageView

© www.soinside.com 2019 - 2024. All rights reserved.