【问题标题】:Why does image in relative layout with "fill_parent" hide button underneath?为什么具有“fill_parent”的相对布局中的图像在下方隐藏按钮?
【发布时间】:2015-05-16 19:34:56
【问题描述】:

我正在尝试在相对布局中增加图像的大小。现在,它的宽度和高度设置为wrap_content。当我将它们设置为fill_parent 时,图像会变大,但会将下方的按钮推离显示屏。相关的 XML 如下。我究竟做错了什么?

编辑:感谢所有答案,但到目前为止,如果我将图像的高度设置为fill_parentmatch_parent,它总是延伸到显示屏的底部,无论按钮是在还是在相对布局之外。为什么相对布局会忽略它下面的内容?

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/myImageView"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/apple"
            android:alpha=".5"
            android:paddingBottom="0dp"
            android:adjustViewBounds="true" />
        <TextView
            android:id="@+id/myImageViewText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Sample"
            android:textColor="#000000"
            android:singleLine="true"
            android:layout_alignLeft="@+id/myImageView"
            android:layout_alignStart="@+id/myImageView"
            android:layout_alignParentTop="true" />


    </RelativeLayout>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/enterPluButton"
        android:background="#FFBD5C"
        android:textColor="#000000"
        android:text="Submit" />
</LinearLayout>

【问题讨论】:

  • 我更新了我的答案。如果您希望它不被 ImageView 向下推,则必须在 Button 上使用 layout_alignBottom 而不是 layout_below。

标签: android image layout fill-parent


【解决方案1】:
  1. 为按钮设置android:layout_alignParentBottom="true"
  2. 为您的 RelativeLayout 设置 android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/enterPluButton"
  3. 为 ImageView 设置 android:layout_width="match_parent" android:layout_height="match_parent"

【讨论】:

  • 感谢尝试,可惜ImageView的布局高度上的match_parent或fill_parent会自动延伸到底部,覆盖按钮。
  • 将父布局改为相对布局
【解决方案2】:

在 RelativeLayouts 中,视图相对于“ViewGroup”的左上角定位。要让 ImageView 停止隐藏 Button 或任何其他视图,请添加 layout_below 属性。

<TextView
    android:layout_below="@+id/relativelayout"
    android:id="@+id/myImageViewText"
    android:layout_width="fill_parent"
    ...
/>

此外,对于 ImageView,除非用于背景,否则不建议您将 layout_height 设置为 fill_parent 或 match_parent。

如果将 Button 放置在相对布局中并将其放置在文本视图下,您将获得所需的结果。

【讨论】:

    【解决方案3】:

    RelativeLayout 位置 View 彼此相对。如果它与另一个子 View 没有关系,则它使用父 View 计算,即 RelativeLayout。这就是为什么你有这个。为什么不将LinearLayout 包裹在ScrollView 中或给RelativeLayout 一个固定尺寸

    【讨论】:

      【解决方案4】:

      我现在明白你想要做什么了。试试这个:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/relativelayout"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">
      
      
          <ImageView
              android:id="@+id/myImageView"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_centerInParent="true"
              android:adjustViewBounds="true"
              android:alpha=".5"
              android:paddingBottom="0dp"
              android:src="@drawable/apple" />
      
          <TextView
              android:id="@+id/myImageViewText"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/myImageView"
              android:layout_alignParentTop="true"
              android:layout_alignStart="@+id/myImageView"
              android:gravity="center"
              android:singleLine="true"
              android:text="Sample"
              android:textColor="#000000" />
      
          <Button
              android:id="@+id/enterPluButton"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignBottom="@id/myImageView"
              android:background="#FFBD5C"
              android:text="Submit"
              android:textColor="#000000" />
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-29
        • 1970-01-01
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        • 2019-01-03
        相关资源
        最近更新 更多