Android 自定义ListView适配器中的ImageButton

Android 自定义ListView适配器中的ImageButton,android,listview,android-adapter,custom-adapter,Android,Listview,Android Adapter,Custom Adapter,自定义列表视图中的两个图像按钮有一些问题。我不是安卓大师,所以我学习了一些关于自定义适配器的教程。我想要的是一个行列表,每行有1个文本视图和2个图像按钮。文本视图也必须是可点击的。这就是我想要的 这是XML代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_wi

自定义
列表视图中的两个图像按钮有一些问题。我不是安卓大师,所以我学习了一些关于自定义适配器的教程。我想要的是一个行列表,每行有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;
}


}
公共类Loogolistadapter扩展了ArrayAdapter{
私有静态最终字符串标记=“LuogoListAdapter”;
私有上下文;
int mresouce;
图像按钮;
图像按钮;
文本视图nameLoc;
公共数据库适配器(上下文、int资源、ArrayList对象){
超级(上下文、资源、对象);
mcontext=上下文;
mresource=资源;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
字符串名称=getItem(位置).GetNomeLoogo();
LayoutFlater充气机=LayoutFlater.from(mcontext);
convertView=充气机。充气(mresource,父项,false);
TextView tvNome=(TextView)convertView.findViewById(R.id.textView2);
tvNome.setText(名称);
posButton=(ImageButton)convertView.findViewById(R.id.SimpleImage按钮);
posButton.setVisibility(View.VISIBLE);
audButton=(ImageButton)convertView.findViewById(R.id.SimpleMapgeButton2);
audButton.setVisibility(View.VISIBLE);
/*posButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
}
});
添加未来行动
audButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
}
});
*/
返回视图;
}
}
但当我运行应用程序时,我看不到我的两个图像按钮:


在布局文件中使用
app:srcCompat=“@drawable/audio”
而不是
tools:srcCompat=“@drawable/audio”
。工具用于android studio中的可视化表示,但不用于实际设备

编辑

因此,如果您在apps build.gradle文件中的
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,检查答案,只需复制并发布答案即可 在这里输入代码

<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" />


您能在xml预览中看到图像吗?我想您忘记了图像按钮中的android:src=“”属性。@tomerpacific,
工具:srccomat=“@drawable/audio
应该适用于资源reference@Farouk我可以在xml设计预览中看到这些图像。如图所示,尝试使用:app:srcCompat=“@drawable/download“。如果它仍然不能正常工作,可能是图像文件的问题。祝你好运我更改了那些LOC,但图像没有显示。现在查看图片。文本被图像按钮覆盖。文本被覆盖,因为您将其设置为固定宽度。添加一个新的答案,它将为您解决所有问题,使用android的默认图像。我认为问题在于我使用的png文件太棒了。请接受我的正确答案