Java Android中存储的drawable将发生更改
恢复时,ImageView背景更改为最后一个大小Java Android中存储的drawable将发生更改,java,android,kotlin,drawable,Java,Android,Kotlin,Drawable,恢复时,ImageView背景更改为最后一个大小 lateinit var bgImage: NinePatchDrawable override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_test_easy) bgImage = ContextCompat
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
}
如果我对每个imageView直接使用drawable,效果很好,但我想存储drawable,因为它是在原始情况下在运行时创建的,需要设置为大量imageView
发生了什么。。。
引用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>
我认为,当系统将bgImage设置为视图时,它将调整bgImage本身的大小。这意味着bgImage将使用上次分配给它的视图的大小 您可以尝试此操作以查看是否所有图像都变大:
iv4.background = bgImage
iv3.background = bgImage
iv2.background = bgImage
iv1.background = bgImage
如果是这种情况,您可以做的是创建四个NinePatchDrawable并重用它们