【问题标题】:GridView: ImageViews/ImageButtons not shown after inflating layoutGridView:膨胀布局后未显示 ImageViews/ImageButtons
【发布时间】:2017-06-04 18:57:16
【问题描述】:

我尝试使用自定义项目布局在 GridView 中显示图块。每个都有一个 TextView 以及下面的三个图像按钮。 XML 项目布局如下所示。在 Android Studio 设计器中,布局看起来完全符合我的要求。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_edit" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_play" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_remove" />
    </LinearLayout>
</LinearLayout>

但是,在我的 GridView 适配器中充气后,TextView 是可见的,但三个图像视图未显示。当我使用图像按钮时也会发生同样的情况。奇怪的是,当我用普通按钮替换这三个图像时,它可以工作。

适配器代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.layout_subject, parent, false);
    float scale = context.getResources().getDisplayMetrics().density;
    v.setLayoutParams(new ConstraintLayout.LayoutParams((int)(180 * scale + .5f), (int)(180 * scale + .5f)));

    return v;
}

定义的 tile 大小 180 x 180 确实足够大,在运行时,图像应该在的地方有足够的空白空间。

知道可能出了什么问题吗?

提前谢谢你

【问题讨论】:

  • 你放了很多layout_weight..
  • 在内部LinearLayout添加weightSum="3"
  • 谢谢,必须是别的东西。我按照建议添加了 weightSum,没有变化。然后我卸下了重量。没有效果。
  • 一种解决方案是将所有矢量可绘制对象更改为 png 或添加矢量支持。如果您确认我可以将其作为您问题的答案
  • 然后在 build.gradle 文件中的 `defaultConfig' 下添加 vectorDrawables.useSupportLibrary = true 并尝试

标签: android android-layout android-gridview


【解决方案1】:

您使用的图片资源是矢量,您需要在 build.gradle 文件中进行一些更改。添加

 vectorDrawables.useSupportLibrary = true

在您的 build.gradle 文件中的 defaultConfig 下。

android {
  defaultConfig {
     ...
     ... 
     vectorDrawables.useSupportLibrary = true  //add this
   }
}

在你的Activity中添加这行代码

 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

现在您可以使用app:srcCompat 而不是android:src 来设置矢量。

【讨论】:

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