【问题标题】:How can I align a ImageView to the right edge of a Button?如何将 ImageView 对齐到 Button 的右边缘?
【发布时间】:2018-03-15 23:18:54
【问题描述】:

我创建了一个简单的布局,其中包含一个按钮、一个 TextView 和一个 ImageView(其中带有一个问号)。

<Button
        android:background="#f00"
        android:textSize="25dp"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button"
        android:id="@+id/button" />


    <TextView
        android:id="@+id/textViewmas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button"
        android:text="title "
        android:textSize="18dp" />

    <ImageView
        android:id="@+id/questionmark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textViewmas"
        android:layout_alignTop="@+id/textViewmas"
        android:layout_alignRight="@+id/button"
        android:layout_alignEnd="@+id/button"
        app:srcCompat="@drawable/questionmark"/>

The question mark should be a bit more right

我希望图像的右边缘(问号)与按钮的右边缘对齐。 另外顶部和底部应与标题的 textView 的顶部和底部对齐(相同高度)。

为什么图像不在右边缘对齐,而是在左边缘? 我注意到如果我增加标题文本大小(例如 30dp),问题就会消失,但我需要一个小文本大小。

--------编辑:图像是否有最小尺寸,在该尺寸下语句不能正常工作?

【问题讨论】:

  • 你到底想要什么,如果你愿意,你可以在油漆中制作图像,然后我会为你设计你想要的。
  • 确保您的图像周围没有任何额外的填充。如果图像有任何额外的填充,请使用 Photoshop 或任何其他工具将其删除。或者,检查这张图片,看看它是否合适。 image.flaticon.com/icons/png/512/78/78299.png
  • 我已经发布了一个解决方案。让我知道它是否有效

标签: java android xml android-layout alignment


【解决方案1】:

你可以使用:

android:layout_alignRight="@+id/button"

或:

android:layout_alignEnd="@+id/button"

然后你可以从左边添加一些边距。

【讨论】:

  • 我已经在我的代码中添加了你的两个对齐语句。左边距没有任何变化。
  • 然后你可以得到你的文本的高度,并将其设置为你的图像高度和高度。
【解决方案2】:

android:scaleType="fitEnd" 添加到您的ImageView

<Button
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="200dp"
    android:textSize="25dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="#f00"
    android:text="Button"/>

<TextView
    android:id="@+id/textViewmas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button"
    android:layout_alignLeft="@+id/button"
    android:text="title "
    android:textSize="18dp" />

<ImageView
    android:id="@+id/questionmark"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/textViewmas"
    android:layout_alignTop="@+id/textViewmas"
    android:layout_alignRight="@+id/button"
    android:layout_alignEnd="@+id/button"
    android:scaleType="fitEnd"
    app:srcCompat="@mipmap/ic_launcher"/>

说明

这就是没有android:layout_alignTop="@+id/textViewmas" 属性时ImageView 的显示方式

当您将layout_alignTop 属性添加到ImageView 时,它会被压缩到TextView 的高度,并且它包含的图像会按比例缩小。默认情况下,ImageViewscaleType 属性似乎是centerInside;它会缩小图像并将其与图像视图的中心对齐,从而在左右两侧增加一些空间。

解决方案是使用android:scaleType="fitEnd" 将缩小的图像移动到图像视图的末尾

输出

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 2017-04-27
    • 2022-01-16
    • 1970-01-01
    • 2011-03-04
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多