【问题标题】:Image loaded in ImageView is hiding the buttonImageView 中加载的图像隐藏了按钮
【发布时间】:2014-07-18 23:54:15
【问题描述】:

当我在ImageView 中加载图像(更具体地说,是纵向形状的图像)时,该图像有时会覆盖Button 上方的ImageViewImageViewlayout_widthlayout_height 设置为 wrap_content。我的要求是 ImageView 将包装内容,但不会掩盖按钮。我不想硬编码像素值。

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="mg.colorfilters.ResultActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/button1"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@color/red"
        android:text="@string/string3"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:onClick="loadImageWithFilterOrSaveImage" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo"
        android:contentDescription="@string/string4" />

</RelativeLayout>

【问题讨论】:

  • 您可以在Button中使用android:layout_above="@+id/imageView1"
  • 有什么办法可以说上面有多少像素?
  • 你不应该使用像素,而是使用dp
  • @Apoorv 我使用 dp。你没有回答我的问题。
  • 使用android:layout_marginBottom=""Button以下的边距

标签: android xml button layout imageview


【解决方案1】:

你想让你的图像居中吗?

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

如果是,那么可能想把你的 Button 代码放在 ImageView 代码下面,比如

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/abc_ab_bottom_solid_dark_holo"
    android:contentDescription="@string/string4" />

<Button
    android:id="@+id/button1"
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@color/red"
    android:text="@string/string3"
    android:textStyle="bold"
    android:textColor="@color/white"
    android:onClick="loadImageWithFilterOrSaveImage" />

这样按钮将始终位于顶部。如果图像很大,按钮将在其顶部可见。仅当您对此行为满意时才使用此解决方案。

【讨论】:

  • 是的,我希望图像位于中心。您的解决方案非常接近我想要的解决方案,但不是确切的解决方案。
  • 您希望图像位于屏幕中心,并且希望按钮位于水平中心且顶部对齐。当图像尺寸增加时会发生什么?有些东西必须让位,否则您必须为图像视图设置最大尺寸
  • 我们可以调整 ImageView 的大小,保持加载图像的纵横比。
  • 是的,我们可以。我认为设置最大尺寸应该注意纵横比。 (如果你这样做记得做 dp-px 计算,因为你需要 dp 中的尺寸,但它是在 px 中设置的)
  • 我已经发现了。使用 android:maxHeight="300dp" 和 android:adjustViewBounds="true"。在 RelativeLayout 中提及相对尺寸的任何方式。如果我使用 layout_weight,我会收到警告。
【解决方案2】:

在您的 Layout.xml 中 请在&lt;ImageView&gt;标签中插入android:layout_above="@+id/button1行。

【讨论】:

    【解决方案3】:

    您可以在相对布局中使用滚动视图,并在滚动视图中使用垂直方向的线性布局...然后在线性布局中添加您的按钮和图像视图.....我没有尝试过,但我认为这会工作:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多