簡體   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