【发布时间】:2019-05-23 16:56:47
【问题描述】:
自定义listView 中的两个图像按钮存在一些问题。我不是安卓大师,所以我学习了一些关于自定义适配器的教程。我想要的是行列表,每行有 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;
}
}
但是当我运行应用程序时,我看不到我的两个图像按钮:
【问题讨论】:
-
你能在 xml 预览中看到图片吗?
-
我想你忘记了图像按钮中的 android:src="" 属性。
-
@tomerpacific,
tools:srcCompat="@drawable/audio应该可以作为资源参考 -
@Farouk 我可以在 xml 设计预览中看到图像。就像这张照片i.stack.imgur.com/mmKOV.png
-
尝试使用:app:srcCompat="@drawable/download"。如果仍然无法正常工作,则问题可能出在您的图像文件上。祝你好运!
标签: android listview android-adapter custom-adapter