您好,感谢您的任何建议。
我需要将 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"因为我需要在运行时更改它。
最佳答案
您可以使用包含 LinearLayout 的 Relativ Layout,然后将 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">
关于Android 如何将 ImageView 重叠在 LienearLayouy 之上,以便它们具有相同的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16855366/