我的视图有问题。如果我试图将图像显示为背景,它占用了整个空间,并且线性布局的第二个子(相邻出现的两个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>
有点晚了,但我的问题用下面的代码解决了。
使用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>
如果有人需要了解更多,我会更新答案。
谢谢。
在LinearLayout
中使用android:background
您必须使用android:background
属性,而不是使用ImageView
。