安卓线性布局背景图片

9
我在显示背景图片时遇到了问题,它会占据整个空间,并且线性布局的第二个子元素(2个相邻的RelativeLayout)无法显示。我应该如何设置背景图片并保持其他布局元素在其上方?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/globe"
        android:layout_gravity="top"
        />

    </RelativeLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >
        </RelativeLayout>


      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >
        </RelativeLayout>

    </LinearLayout>


</LinearLayout>
3个回答

27

虽然有点晚了,但我的问题通过以下代码得以解决。

使用ImageView设置我的背景图片, 然后在其上方显示线性布局.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:contentDescription="@string/my_msg"
        android:src="@drawable/my_super_bg_image" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/top_style"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/today"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="some text here"
                android:textSize="24sp" />
        </RelativeLayout>

    </LinearLayout>

</FrameLayout>
如果有人需要了解更多,我会更新答案。
谢谢。

iv不应该使用match_parent,例如在布局比图像drawable bg大的情况下使用fitxy。 - ceph3us
1
在我的情况下,我还需要为ImageView添加android:scaleType="centerCrop" - Tigran Babajanyan

5
在您的LinearLayout中, 使用android:background

我认为这个答案仍然可以帮助那些有特定分辨率想法并且能够进行一些图像编辑的人。 - lolsky

0

你需要使用android:background属性,而不是使用ImageView


5
在其他地方提到过,当使用这个属性时,图片会被拉伸和挤压以适应设备屏幕。我希望图片居中且不被拉伸和挤压。 - user3141985

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接