如何将一个TextView与另一个TextView的右边对齐,该TextView会中断到下一行? [英] How to align a TextView to the right of another TextView which breaks to the next line?
问题描述
我遇到的问题是将第二个TextView
对齐到TextView
的右侧,该范围跨越两行.
The problem I am having is aligning the second TextView
to the right of the TextView
which spans to two lines.
我正在使用RelativeLayout
,但是当我使用layout_toRightOf
时,它会出现在屏幕顶部,而当我添加layout_alignRight
时,它会消失.
I am using a RelativeLayout
, but when I use layout_toRightOf
it appears at the top of the screen, and when I add layout_alignRight
it disappears.
我对此非常困惑.您将假定它将跟随第一个TextView
的结束位置,但不会结束.哦,如果有人认为这是问题,我会在宽度和高度上使用wrap_content
.预先感谢.
I am very confused how this works. You would assume that it would follow where the first TextView
ends, but it does not. Oh, and I am using wrap_content
on width and height incase anyone thinks that's the problem. Thanks in advance.
XML
<TextView
android:id="@+id/edit_event_name_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:scrollHorizontally="false"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/show_event_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:scrollHorizontally="false"
android:layout_toRightOf="@+id/edit_event_name_show"
android:layout_alignBaseline="@+id/edit_event_name_show" />
编辑:对于尝试将文本与TextViews很好地对齐时遇到类似问题的任何人,默认情况下都没有办法.但是您可以使用SpannableString
并使用SpannableStringBuilder
进行构建.
EDIT: To anyone that ever has a similar issue with trying to align things nicely with TextViews, there is not a way by default. But you can use SpannableString
and build them using SpannableStringBuilder
.
推荐答案
第三次尝试(成功尝试)
我相信这会为您提供帮助,并且比其他方法容易得多.
I believe this will help you and be much easier than the other stuff.
Try using this reference, but change font size instead of color.
Sam的答案总结了链接. (如果链接在某个时候死了.)
Sam's answer sums up the link. (in case the link dies at some point.)
第二次尝试
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/edit_event_name_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:scrollHorizontally="false"
android:text=" test 1 jfalksjdflkjasdfj"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="@+id/show_event_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_event_name_show"
android:layout_alignParentRight="true"
android:scrollHorizontally="false"
android:text="test2 adjfalsjdfla fa sdfasdf asf a"
android:textSize="10dp" />
</RelativeLayout>
图片:
Picture:
首次尝试
为什么不用LinearLayout
包围TextViews
.这样可以使他们井井有条,干净一点.
Why not surround your TextViews
with a LinearLayout
. It will keep them better organized and a little cleaner.
尝试一下,让我知道怎么回事:
Try this and let me know how it goes:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/edit_event_name_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:text="test1"
android:textSize="18dp"
android:textStyle="bold"
android:layout_weight="1" />
<TextView
android:id="@+id/show_event_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:text="test2"
android:textSize="10dp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
图片:
Picture:
长文本:
这篇关于如何将一个TextView与另一个TextView的右边对齐,该TextView会中断到下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!