繁体   English   中英

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

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

我尝试使用自定义项目布局在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确实足够大,在运行时,图像应有足够的空白空间。

知道有什么问题吗?

先感谢您

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

 vectorDrawables.useSupportLibrary = true

在build.gradle文件的defaultConfig下。

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

在您的活动中添加以下代码行

 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

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

暂无
暂无

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

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