简体   繁体   English

GridView:放大布局后未显示ImageViews / ImageButtons

[英]GridView: ImageViews/ImageButtons not shown after inflating layout

I try to display tiles within a GridView using a custom item layout. 我尝试使用自定义项目布局在GridView中显示图块。 Each has a TextView as well as three image buttons below. 每个按钮都有一个TextView以及下面的三个图像按钮。 The XML item layout looks like this. XML项目布局如下所示。 Within the Android Studio designer, the layout looks exactly the way I want it. 在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>

However, after inflating within my GridView adapter, the TextView is visible but the three image views are not shown. 但是,在我的GridView适配器中膨胀后,TextView是可见的,但未显示三个图像视图。 Same happens when I use image button. 当我使用图像按钮时,也会发生同样的情况。 Curiously, when I replace the three images by a normal button, it works. 奇怪的是,当我用一个普通按钮替换这三个图像时,它可以工作。

Adapter code: 适配器代码:

@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;
}

The defined tile size 180 x 180 is indeed large enough, at runtime, there's plenty of empty space where the images should be. 定义的图块大小180 x 180确实足够大,在运行时,图像应有足够的空白空间。

Any idea what could be wrong? 知道有什么问题吗?

Thank you in advance 先感谢您

The image assets you are using are vectors and you need to make some changes in your build.gradle file. 您使用的图像资产是矢量,您需要在build.gradle文件中进行一些更改。 add

 vectorDrawables.useSupportLibrary = true

under defaultConfig in your build.gradle file. 在build.gradle文件的defaultConfig下。

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

In your Activity add this line of code 在您的活动中添加以下代码行

 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

now you can set vector using app:srcCompat instead of android:src . 现在您可以使用app:srcCompat而不是android:src设置矢量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM