【问题标题】:Android Widget ImageView not showAndroid Widget ImageView 不显示
【发布时间】:2017-07-17 02:30:59
【问题描述】:

我只是启动一个小部件,它的 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>

即使我使用硬代码 dp 或其他选项放入线性布局或相对布局,甚至更改其他项目图片,这在应用程序内都可以,但没有显示任何内容。我只是想知道它不显示的原因是什么。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    我又找到了一种选择。您可以设置 ImageView 的背景以显示您的可绘制对象,而不是设置“src”。同样,仍然不是很好的解决方案,但是 drawable 是可见的。

    <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。我猜同样的事情......
    【解决方案2】:

    尝试使用 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"/>
    

    【讨论】:

    • 感谢您的回复。我尝试使用 src 更改所有图像视图,但仍然无法在小部件中使用。我发现这是对活动的工作,但更改为小部件,它仍然是空白的
    【解决方案3】:

    LinearLayout 将绘制在ImageView 上方,因此不可见。

    尝试在ImageView下方设置LinearLayout

    <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">
    

    【讨论】:

    • 抱歉回复晚了,我试过这种方式,还是没有渲染出来。
    【解决方案4】:

    在我的情况下,当图像的可绘制对象包含与主题相关的颜色时,小部件上没有显示图像,例如:

    android:fillColor="?attr/themeColorBase80"
    

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

    如果是这样,请使用明确定义的属性,例如:

    android:fillColor="#D5D5D5"
    

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

    【讨论】:

      【解决方案5】:

      使用

      android:src="@drawable/ic_media_next"
      

      而不是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        • 1970-01-01
        • 2017-08-02
        • 1970-01-01
        • 2011-07-26
        • 1970-01-01
        相关资源
        最近更新 更多