更改LinearLayout的背景会改变它的宽度和高度

问题描述:

我试图将LinearLayout的背景更改为某些木材纹理,但它会根据木制背景的尺寸更改LinearLayout的宽度和高度! 我需要LinearLayout忽略背景尺寸!更改LinearLayout的背景会改变它的宽度和高度

<LinearLayout 
    android:id="@+id/LinearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/wood" 
    android:orientation="vertical" > 

some content here which I need to wrap height to them! 

</LinearLayout> 
+1

可能是有用的:http://*.com/questions/8202647/setting-a-background-image-to-a-view-stretches-my-view – 2014-11-01 08:44:17

您的LinearLayout's高度为"wrap_content"。这意味着它需要内容的高度(你的背景)。您可以将其更改为"match_parent"或在dp中给它一个固定大小。所以即使背景也一样。

+0

但我希望它的大小,以我的真实内容,不是背景。 – AVEbrahimi 2014-11-01 08:40:33

+1

根据你的布局给你的父母布局一个“weightSum”和它的孩子“重量”。使所有儿童的身高达到0dp。 – 2014-11-01 08:42:18

+0

欲了解更多信息,http://*.com/questions/7452741/what-is-androidweightsum-in-android-and-how-does-it-work – 2014-11-01 08:43:27

如果您希望它是固定的,可以在dp中使用固定的高度和宽度,也可以动态计算旧背景的高度和宽度,然后应用新的背景并将LinearLayout的高度和宽度设置为您以编程方式计算的大小。