更改背景是否也会更改 LinearLayout 的填充? [英] Does changing the background also change the padding of a LinearLayout?
问题描述
我有以下 LinearLayout
.我不明白的是,如果我将背景设置为另一个图像,填充信息将被重置.有没有办法防止这种情况发生?
I have the following LinearLayout
. What I don't understand is if I set the background to another image, the padding information are reset. Is there a way to prevent this?
<LinearLayout android:id="@+id/aPanel"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="@drawable/bkground"
android:paddingLeft="15dp" android:paddingRight="15dp">
<!-- some children here -->
</LinearLayout>
当我更改 LinearLayout
aPanel 的背景可绘制对象时,我看到孩子的位置发生了变化.
I see the position of the children get shifted when I change the background drawable of the LinearLayout
aPanel.
推荐答案
这是在 View
上更改背景 Drawable
时的默认行为.据 Android 开发人员之一 Romain Guy 表示,设置图像会重置填充的原因是因为 9-patch 图像可以对填充进行编码."在类似问题中查看他的完整回答.
This is the default behavior when changing the background Drawable
on a View
. According to Romain Guy, one of the Android developers, "The reason why setting an image resets the padding is because 9-patch images can encode padding." See his full answer in a similar question.
解决方法是在每次更改背景可绘制对象时重置代码中的填充.
The fix is to reset the padding in code each time you change the background drawable.
这篇关于更改背景是否也会更改 LinearLayout 的填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!