GridView:在展开布局后未显示ImageViews / ImageButtons

时间:2017-06-04 10:03:17

标签: android android-layout android-gridview

我尝试使用自定义项目布局在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;
}

定义的瓷砖尺寸180 x 180确实足够大,在运行时,图像应该有足够的空白空间。

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

提前谢谢

1 个答案:

答案 0 :(得分:1)

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

 vectorDrawables.useSupportLibrary = true

在build.gradle文件中的 defaultConfig 下。

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

在您的活动中添加此行代码

 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

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

相关问题