【问题标题】:ImageButton in a custom ListView adapter自定义 ListView 适配器中的 ImageButton
【发布时间】: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


【解决方案1】:

在您的布局文件中使用app:srcCompat="@drawable/audio" 而不是tools:srcCompat="@drawable/audio"。 tools 用于 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

【讨论】:

  • 我更改了这些位置,但未显示图像。看图imgur.com/a/14tR8Kf现在文字被图片按钮覆盖了
  • 好吧,文本被覆盖了,因为您将其设置为固定宽度。添加一个新的答案,将为您处理一切
  • 使用android默认图片解决。我认为问题在于我使用的 png 文件
  • 哦,太好了。请接受我的答案作为正确答案
【解决方案2】:

请仔细检查答案,只需复制并发布答案 在此输入代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多