简体   繁体   English

存储的可绘制对象在 Android 中发生了变化

[英]Stored drawable become changed in Android

ImageView background chanded to last one size while onResume ImageView 背景在 onResume 时更改为最后一个尺寸

lateinit var bgImage: NinePatchDrawable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_easy)

        bgImage = ContextCompat.getDrawable(this, R.drawable.tm_bg_special) as NinePatchDrawable

        iv1.background = bgImage
        iv2.background = bgImage
        iv3.background = bgImage
        iv4.background = bgImage

        // It works fine if I use drawable directly to each imageView
        iv1.background = ContextCompat.getDrawable(this, R.drawable.tm_bg_special) as NinePatchDrawable
        iv2.background = ContextCompat.getDrawable(this, R.drawable.tm_bg_special) as NinePatchDrawable
        iv3.background = ContextCompat.getDrawable(this, R.drawable.tm_bg_special) as NinePatchDrawable
        iv4.background = ContextCompat.getDrawable(this, R.drawable.tm_bg_special) as NinePatchDrawable
    }

It works fine if I use drawable directly to each imageView but I want to store the Drawable because it is creadted in runtime in orginal situation and need set to large number of imageView如果我直接将 drawable 用于每个 imageView,它工作正常,但我想存储 Drawable,因为它是在原始情况下在运行时创建的,并且需要设置为大量的 imageView

What happen visually...视觉上会发生什么... 在此处输入图片说明

Referance xml参考xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        android:layout_margin="5dp"
        android:visibility="visible"
        android:layout_alignParentBottom="true">

        <ImageView
            android:id="@+id/iv1"
            android:layout_width="90dp"
            android:padding="0dp"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/iv2"
            android:layout_width="50dp"
            android:padding="0dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/iv3"
            android:layout_width="120dp"
            android:padding="0dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/iv4"
            android:layout_width="20dp"
            android:padding="0dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"/>

    </LinearLayout>

I think while the system sets the bgImage to views it will resize the bgImage itself.我认为当系统将 bgImage 设置为视图时,它会调整 bgImage 本身的大小。 That means the bgImage will be using the size of the view that it is last assigned to.这意味着 bgImage 将使用上次分配给它的视图的大小。

You can try this to see if all the image becomes large:你可以试试这个,看看是否所有的图像都变大了:

    iv4.background = bgImage
    iv3.background = bgImage
    iv2.background = bgImage
    iv1.background = bgImage

If that's the case, what you can do is to create four NinePatchDrawable and reuse them.如果是这种情况,您可以做的是创建四个 NinePatchDrawable 并重复使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM