自定义 ListView 适配器中的 ImageButton

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

我在自定义

listView
中的两个图像按钮时遇到一些问题。我不是 Android 大师,所以我遵循了一些关于自定义适配器的教程。我想要的是一个行列表,每行有 1 个文本视图和 2 个图像按钮。文本视图也必须是可点击的。这就是我想要的。

这是 XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >


<TextView
    android:id="@+id/textView2"
    android:layout_width="239dp"
    android:layout_height="61dp"
    android:gravity="center"
    android:text="TextView"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2" />


<ImageButton
    android:id="@+id/simpleImageButton"
    android:layout_width="62dp"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="0dp"
    android:scaleType="fitCenter"

    tools:srcCompat="@drawable/download" />

<ImageButton
    android:id="@+id/simpleImageButton2"
    android:layout_width="62dp"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="68dp"
    android:layout_marginBottom="0dp"

    android:scaleType="fitCenter"
    tools:srcCompat="@drawable/audio" />

这是适配器的java类:

public class LuogoListAdapter extends ArrayAdapter<Luogo> {
private static final String TAG = "LuogoListAdapter";
private Context mcontext;
int mresouce;
ImageButton posButton;
ImageButton audButton;
TextView nameLoc;

public LuogoListAdapter( Context context, int resource, ArrayList<Luogo> objects) {
    super(context, resource, objects);
    mcontext = context;
    mresouce = resource;

}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    String name = getItem(position).getNomeLuogo();
    LayoutInflater inflater = LayoutInflater.from(mcontext);
    convertView = inflater.inflate(mresouce, parent, false);
    TextView tvNome = (TextView) convertView.findViewById(R.id.textView2);
    tvNome.setText(name);
    posButton = (ImageButton) convertView.findViewById(R.id.simpleImageButton);
    posButton.setVisibility(View.VISIBLE);
    audButton = (ImageButton) convertView.findViewById(R.id.simpleImageButton2);
    audButton.setVisibility(View.VISIBLE);
    /*posButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
    ADD FUTURE ACTION
    audButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {

        }
    });
    */
    return convertView;
}


}

但是当我运行应用程序时,我看不到我的两个图像按钮:

android listview android-adapter custom-adapter
2个回答
1
投票

在布局文件中使用

app:srcCompat="@drawable/audio"
而不是
tools:srcCompat="@drawable/audio"
。工具用于 android studio 中的视觉表示,但不用于实际设备上

编辑

因此,如果您在应用程序的 build.gradle 文件中设置了以下内容,则 app:srcCompat 可以工作

defaultConfig { vectorDrawables.useSupportLibrary = true }

下面是我如何实现我的布局

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="TextView"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
    app:layout_constraintTop_toTopOf="@id/simpleImageButton"
    app:layout_constraintBottom_toBottomOf="@id/simpleImageButton"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@id/simpleImageButton" />

<ImageButton
    android:id="@+id/simpleImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    app:layout_constraintStart_toEndOf="@id/textView2"
    app:layout_constraintEnd_toStartOf="@id/simpleImageButton2"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:srcCompat="@drawable/ic_arrow_back_black_24dp"
    tools:ignore="ContentDescription" />

<ImageButton
    android:id="@+id/simpleImageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    app:layout_constraintStart_toEndOf="@id/simpleImageButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:srcCompat="@drawable/ic_arrow_back_black_24dp"
    tools:ignore="ContentDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果您希望图像按钮没有灰色背景,则使用 android:background="@drawable/download" 代替 app:srcCompat


0
投票

请检查答案,复制并发布答案即可

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
    android:id="@+id/textView2"
    android:layout_width="239dp"
    android:layout_height="61dp"
    android:gravity="center"
    android:text="TextView"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
<ImageButton
    android:id="@+id/simpleImageButton"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="0dp"
    android:scaleType="fitCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"        
    android:src="@drawable/download" />
<ImageButton
    android:id="@+id/simpleImageButton2"
    android:layout_width="62dp"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="68dp"
    android:layout_marginBottom="0dp"
    android:scaleType="fitCenter"
    android:src="@drawable/audio" />
© www.soinside.com 2019 - 2024. All rights reserved.