我在自定义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;
}
}
但是当我运行该应用程序时,我看不到我的两个图像按钮:
答案 0 :(得分: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。答案 1 :(得分: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" />