【问题标题】:ImageView isn't shown when added to LinearLayout dynamically动态添加到 LinearLayout 时不显示 ImageView
【发布时间】:2017-07-08 06:30:39
【问题描述】:

我正在使用支持库compile 'com.android.support:support-v4:25.1.1' 当我尝试将视图动态添加到其他布局时,图像丢失。

app:srcCompat="@drawable/ic_remove_task" 在图像视图中使用时不显示图像。

当我这样写android:src="@drawable/ic_remove_task"

可能出了什么问题?

这是 ImageView

<ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/ic_remove_task"
        android:id="@+id/remove_sub_task_icon" />

代码在这里,来自上一个问题: Adding view to LinearLayout dynamically

【问题讨论】:

  • 显示完整代码。那里可能发生了很多事情。您是否设置了 LinearLayout 的大小?你在哪里添加的?确定添加了吗?
  • 是图片矢量吗?
  • @JonZarate,我编辑了一个问题并在上面添加了代码。
  • @Pulkit,是的,图片是矢量。

标签: android android-layout dynamic view android-support-library


【解决方案1】:

从 Android 支持库 23.3.0 开始,支持矢量可绘制对象只能通过 app:srcCompat 加载

您需要将 vectorDrawables.useSupportLibrary = true 添加到您的 build.gradle 文件中

    // Gradle Plugin 2.0+  
 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }  

并确保应用使用正确的命名空间,如下所示:

xmlns:app="http://schemas.android.com/apk/res-auto"

【讨论】:

  • 我的 gradle 代码中已经有了这个(defaultConfig { vectorDrawables.useSupportLibrary = true }
  • 检查xmlns:app命名空间的xml文件
  • 在布局中?或图片
  • 在这行代码xmlns:android="http://schemas.android.com/apk/res/android"之后的布局中添加xmlns:app="http://schemas.android.com/apk/res-auto"
  • 没什么(没有结果。
【解决方案2】:

我已经自己解决了。问题在于夸大视图。 你必须像这样初始化充气机:

LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

而不是这个:

 LayoutInflater layoutInflater = (LayoutInflater) activity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多