安卓小部件 ImageView 无法显示

6

我刚开始创建一个小部件,它的xml在AS模拟器上显示了imageview,但无论我把imageview放在哪个位置,图片都不会显示。以下是我的小部件的xml布局:

<RelativeLayout 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:background="#333"
android:padding="@dimen/widget_margin">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@android:drawable/ic_popup_sync"
    android:id="@+id/imageView"
    android:layout_alignParentLeft="true"/>

<LinearLayout
    android:orientation="horizontal"
    android:layout_alignLeft="@+id/imageView"
    android:layout_alignRight="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="10">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4">

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

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

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

    <TextView
        android:text="16:00"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/textView5"
        android:textSize="20sp"
        android:layout_weight="3" />

    <TextView
        android:text="14:00"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:id="@+id/textView6"
        android:layout_weight="1" />

</LinearLayout>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    app:srcCompat="@android:drawable/ic_media_next"
    android:id="@+id/imageView2" />
</RelativeLayout>

模拟器

在测试手机上

即使我将其放置在LinearLayout或RelativeLayout中,并使用硬代码DP或其他选项进行更改,甚至更改为另一个项目的图片在应用程序中也可以正常显示,但仍然未显示任何内容。 我想知道这是什么原因导致它不显示。

5个回答

10

我发现另外一种选项。您可以将ImageView的背景设置为显示可绘制对象,而不是设置'src'。再次强调,这并不是非常好的解决方案,但是可绘制对象是可见的。

<ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@android:drawable/ic_popup_sync"
   android:id="@+id/imageView"
   android:layout_alignParentLeft="true"/>

我所做的是使用FrameLayoutandroid:background属性,而不是ImageView,这样就可以使其正常工作。我想应该是一样的... - Andrei Fecioru

3

尝试使用android:src代替app:srcCompat。我不确定这是否是最好的解决方案,但对我有效。

<ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@android:drawable/ic_popup_sync"
   android:id="@+id/imageView"
   android:layout_alignParentLeft="true"/>

谢谢回复。我尝试将所有的ImageView更改为src,但在小部件中仍然无法工作。我发现这在Activity上可以运行,但是更改为小部件后,它仍然是空白的。 - CbL

3

使用

android:src="@drawable/ic_media_next"

替代

app:srcCompat="@android:drawable/ic_media_next"

1
LinearLayout会被绘制在ImageView上方,因此不可见。
尝试将LinearLayout设置为在ImageView下方。
<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:srcCompat="@android:drawable/ic_popup_sync"
  android:id="@+id/imageView"
  android:layout_alignParentLeft="true"/>

 <LinearLayout
  android:layout_below="@+id/imageView" //add this to set this layout below imageview
  android:orientation="horizontal"
  android:layout_alignLeft="@+id/imageView"
  android:layout_alignRight="@+id/imageView2"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:weightSum="10">

抱歉回复晚了,我尝试了这种方法,但仍然无法渲染任何内容。 - CbL

0
在我的情况下,当图像的可绘制对象包含主题相关颜色时,小部件上不会显示图像,例如:
android:fillColor="?attr/themeColorBase80"

请检查一下你的drawable/ic_popup_sync和drawable/ic_media_next是否包含任何隐式定义的属性,以及其他属性。

如果有,请使用显式定义的属性,例如:

android:fillColor="#D5D5D5"

或者检查任何其他属性,不仅仅是颜色。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接