包括一个背景可绘制的foobars,我在Android中的布局。为什么?

包括一个背景可绘制的foobars,我在Android中的布局。为什么?,android,layout,background,android-linearlayout,Android,Layout,Background,Android Linearlayout,如果我的根布局中没有包含背景,则所有内容都会按预期呈现: 但是,如果我只在背景中添加一行,则布局会完全混乱: XML布局的相关位是: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width ="fill_parent" android:layout_height="fill_parent" android:paddingRight

如果我的根布局中没有包含背景,则所有内容都会按预期呈现:

但是,如果我只在背景中添加一行,则布局会完全混乱:

XML布局的相关位是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width ="fill_parent"
    android:layout_height="fill_parent"
    android:paddingRight ="20dip" 
    android:paddingLeft  ="20dip"
    android:gravity      ="center_vertical"
    android:background   ="@drawable/main_background">


从字面上看,上述两个截图之间的唯一区别是包含了
android:background=“@drawable/main_background”
。如果你能帮我的话,我会给你一块饼干:)

至于你为什么会在背景中体验到你现在的样子,可能是你的背景图像被重力吸引住了,但这只是一个猜测


作为一种解决方法,将整个内容包装在
框架布局中。将
FrameLayout
的第一个子项设置为具有背景的
ImageView
。将
FrameLayout
的第二个子项设置为现有的
LinearLayout
(sans
android:background
)属性。由于
FrameLayout
RelativeLayout
允许堆叠,这将导致您的线性布局显示在图像上方,并应为您提供所需的视觉效果。

伙计,这很烦人。我希望我能弄明白为什么我所做的不起作用。不过你的解决方案很有效,谢谢。我也有类似的问题,花了几个小时想知道为什么。我一直不知道为什么背景图像会这样。对我来说,在运行时它们看起来是正确的。布局只有在设计者看来才糟糕。