1

嗨,感谢您的任何建议。

我需要将 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 缩小为单行) 在此处输入图像描述

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

关闭,但 ImageView 现在扩展以填充所有可用空间,并且不会像我需要的那样“包装内容”。

在此处输入图像描述

期望的结果是这样的: 在此处输入图像描述

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

4

2 回答 2

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">        
于 2013-05-31T10:50:36.630 回答
1

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

为什么不?使用setBackgroundResource

于 2013-05-31T11:00:11.533 回答