【问题标题】:Custom DialogPreference not showing ImageView properly自定义 DialogPreference 未正确显示 ImageView
【发布时间】:2018-11-18 10:49:54
【问题描述】:

我有一个自定义 DialogPreference:

    <com.myapp.MyPreferenceDialog
        android:key="dialog_key"
        android:title="Test MyDialogPreference"
        android:positiveButtonText="ok"
        android:negativeButtonText="cancel"
        />

对话框代码如下:

class MyDialogPreference extends DialogPreference
{

    public MyDialogPreference( Context context, AttributeSet attrs )
    {
        super(context, attrs);
        setDialogLayoutResource( R.layout.test );
    }
}

布局xml代码如下:

<?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:orientation="vertical">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/image" />
</LinearLayout>

当我启动应用程序设置并触摸“Test MyDialogPreference”时,会显示一个对话框,但 imageview 不显示任何图像。我知道如何以编程方式解决此问题(只需覆盖 onBindDialogView() 并将 ImageView 图像资源设置为其可绘制对象),但我想了解为什么我必须以编程方式进行。

【问题讨论】:

  • 您是否尝试将LinearLayout 的大小更改为wrap_content?
  • @Pawel 是的,这并不能解决问题。

标签: android dialog-preference


【解决方案1】:

将以下内容添加到您的 build.gradle 文件中

defaultConfig {  
 vectorDrawables.useSupportLibrary = true  
}  

【讨论】:

  • 不工作。在这个例子中我没有使用矢量绘图。
  • 尝试使用 android:src="" 而不是 app:srccompat
  • 使用 android:src= 有效!似乎 DialogPreference 不适用于 app:srcCompat。谢谢。
  • 很高兴它有帮助...你可以接受答案!
  • 是的,但是你应该更新它,因为解决方案是 android:src="..." 而不是更新 gradle 文件。
猜你喜欢
  • 1970-01-01
  • 2011-12-30
  • 2016-10-02
  • 1970-01-01
  • 2019-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多