我对应用程序开发以及与Android Studio和Layout打交道还比较陌生.我正在设计我的应用程序,遇到了一个问题.我有一个ListView的ListItem的设计,其中球员的名字和2个图标(编辑和删除)应该显示.因此,当用户添加球员时,条目应该是这样显示的.

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

ListItem Design, created with Figma

我认为可以将整个设计设计为XML中的按钮,然后将此设计作为背景.但接下来的问题是,我如何才能使图标可点击?

或者有没有更好的办法,让我错过这里?

推荐答案

首先,不要使用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>

别忘了在ImageViewsrc属性中添加图像的路径.

然后,在您的ViewHolder中,您可以在每个ImageView上找到并设置onClickListener

Kotlin相关问答推荐

如何在Jetpack Compose中的列中渲染图像

在KMM合成多平台中创建特定于平台的视图

某些公共函数显然不能在类实例上访问;Klaxon示例

Microronaut Data 4和JDbi

协程子作业(job)取消

Android Jetpack Compose:在空的 Compose 活动中找不到 OutlinedTextField (Material3)

使用事务时未调用 Kafka ConsumerInterceptor onCommit

为什么多线程不会使执行更快

如何缩短 MaterialTheme.colors.primary?

如何从 kotlin 中的数据类访问 val?

T except one class

Kotlin 从其他类调用成员扩展函数

Kotlin 1.2.21 + SimpleXml 2.3.0 - consume List error (must mark set get method)

IntentService (kotlin) 的默认构造函数

在 gradle android library kotlin 项目中禁用 META-INF/* 生成

大小写敏感性 Kotlin / ignoreCase

(kotlin的Moshi)@Json vs@field:Json

Kotlin - 是否可以在类中的 init 块之前初始化伴随对象?

Kotlin:测试中的 java.lang.NoSuchMethodError

Kotlin中对象和数据类的区别是什么?