Android如何在LienearLayouy上重叠ImageView,使它们具有相同的大小?

时间:2013-05-31 10:38:16

标签: android layout imageview

嗨,谢谢你的任何建议。

我需要将ImageView放在LinearLayout上重叠。

我试过这个:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

 >

<ImageView
android:id="@+id/backgroundimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/gradient_box"
android:layout_gravity="center"
 />


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:layout_gravity="center">

但这是我得到的结果(ImageView缩小到一行) enter image description here

我尝试过使用RelativeLayout,这是我得到的结果。

关闭,但ImageView现在扩展为填充所有可用空间,并且不会根据需要“包装内容”。

enter image description here

所需的结果如下: enter image description here

PS:我不能使用android:background =“image”,因为我需要在运行时更改它。

2 个答案:

答案 0 :(得分:1)

您可以使用包含LinearLayout的Relativ布局,然后将ImageView与LinearLayout对齐

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/background"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

   <LinearLayout
    android:id="@+id/linear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:layout_gravity="center">

   <ImageView
     android:id="@+id/backgroundimage"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/gradient_box"
     android:layout_alignLeft="@id/linear"
     android:layout_alignRight="@id/linear"
     android:layout_alignTop="@id/linear"
     android:layout_alignBottom="@id/linear"
     android:layout_gravity="center"
     android:scaleType="fitCenter" />

如果您只想将图像设置为背景,则可以为线性布局设置背景图像:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/background"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/blue" >

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_centerVertical="true"
   android:orientation="vertical"
   android:background="@drawable/gradient_box"
   android:layout_gravity="center">        

答案 1 :(得分:1)

  

PS:我不能使用android:background =“image”,因为我需要在运行时更改它。

为什么不呢?使用setBackgroundResource