Android:对文本后面的图像使用alignBaseline
下面是一个 TextView,后跟一个包含在relativelayout中的imageview。我试图使图像的底部与文本的基线对齐。当我对图像使用alignBaseline时,引用TextView,图像的顶部与文本的基线对齐,而不是底部。我该如何解决这个问题?
<TextView
android:id="@+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="feb"
android:textSize="60px"
android:typeface="serif"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="15px"
android:layout_toRightOf="@id/month"
android:layout_alignBaseline="@id/month"
android:src="@drawable/minicalimage"
android:id="@+id/calimage"
/>
Below is a TextView followed by an ImageView contained in RelativeLayout. I'm trying to get the bottom of the image to align with the baseline of the text. When I use alignBaseline for the image,reference the TextView, it is the top of the image that aligns with the baseline of the text instead of the bottom. How can I fix this?
<TextView
android:id="@+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="feb"
android:textSize="60px"
android:typeface="serif"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="15px"
android:layout_toRightOf="@id/month"
android:layout_alignBaseline="@id/month"
android:src="@drawable/minicalimage"
android:id="@+id/calimage"
/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从 API11 开始你可以简单地使用
android:baselineAlignBottom="true"
在 API11 之前,您可以覆盖
getBaseLine()
并返回图像高度。Since API11 you can simply use
android:baselineAlignBottom="true"
Prior to API11 you can overwrite
getBaseLine()
and return image height.您应该在 ImageView 中使用以下两行将 Image 视图的底部与附近 TextView 的底部对齐:
You should use both following lines in your ImageView to align bottom of Image view to bottom of nearby TextView:
我能够完成我想做的事情。我没有使用alignBaseline,只是解决了填充和调整图像大小的问题。我遇到的一些问题是由于图像太大并且它不合需要地填充了空间。我用来实现我想要的代码如下:
这就是结果:
I was able to accomplish what I wanted to do. I did not utilize alignBaseline, just fixed the problem with padding and resizing the image. Some of the issues I was having arose from having an image too big and it filling up the space undesirably. The code I used to achieve what I wanted is below:
This was the result:
我偶然发现了一个类似的问题,并通过完全避免
layout_alignBaseline
来解决它,而是依靠layout_alignBottom
,它从 API 1 开始就可用。I stumbled upon a similar issue, and solved it just by avoiding
layout_alignBaseline
altogether, relying onlayout_alignBottom
instead, which is available since API 1.我使用文本视图而不是图像视图,并使用新文本视图的可绘制对象来设置图像并且它有效:
i used a text view instead of an image view and used the new text view's drawable to set the image and it worked:
您想要使用 android:layout_alignBottom 属性将 ImageView 与另一个 View 的基线对齐。
在下面的示例中,我将 ImageView 与另一个 TextView 对齐。我包含了 ImageView 的代码,而不是 TextView 的代码。为了与 TextView 对齐,您只需要包含 TextView 的 id 即可。
如果用户更改设备上的文本大小,调整边距或填充将导致视图未对齐。
You want to use the android:layout_alignBottom attribute to align the ImageView with the baseline of another View.
In the sample below I aligned the ImageView to another TextView. I included the code for the ImageView, and not the TextView. In order to align to the TextView you'll just need to include the id of the TextView.
Adjusting the margin or padding will result in the Views being misaligned if the user changes the text size on their device.