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

标签 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"因为我需要在运行时更改它。

最佳答案

您可以使用包含 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/

相关文章:

android - 在 MAC 操作系统上找不到 Android SDK 管理器设置

html - CSS Sticky Footer Menu - 检查元素时布局损坏

java - Android:将选择的不同布局动态加载到另一个布局中

java - 如何将 ImageView 列表设置为 RecyclerView

php - Android Http Post 不起作用

java - onStart 方法中的 NoClassDefFoundError - Android

android - 缩放图像以适合 android ImageView 中的顶部和右侧

android - 从使用 Picasso 加载的 ImageView 获取位图

java - 为给定位置精度的 Google map 确定合理的缩放级别

html - 如何在 tile 中 float 不同大小的元素