问题描述
当前我的布局如下:
布局代码:
<TextView
style="@style/style_textviewsProfile"
android:id="@+id/textViewBusID"
android:text="Bus ID: " />
<style name="style_textviewsProfile">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/Black</item>
<item name="android:textSize">20dp</item>
<item name="android:layout_marginTop">10dp</item>
</style>
但是我想将文本调整到textviews的另一个角落,例如公交车ID 到最左侧, as-676 到最右侧.我需要对XML文件进行哪些更改?
But I want to adjust the text to one and other corner of the textviews, e.g. Bus ID to left-most and as-676 to right-most. What changes will I need in my XML file?
推荐答案
您可以添加一个具有水平方向的相对布局,并在其中添加两个文本视图,然后配置其方向.例如;
You can add one relative layout with horizontal orientation and add two text views inside it and then configure their orientation.For example;
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_height="wrap_content"
android:text="Name:"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:text="Tiwari Ji"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
将一个TextView对准父级的开头,将另一个TextView对准父级的末尾,应该会为您提供所需的结果.
Aligning one TextView to start of parent, other one to the end of parent should give you the desired result.
您可以在屏幕上看到上面的代码呈现的内容:文本正确对齐
You can see what the above code renders on screen:Properly Aligned text
使用LinearLayout也可以实现同样的壮举
The same feat can also be achieved using LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_margin="16dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Name:" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="right"
android:text="Tiwari Ji"
android:layout_height="wrap_content" />
</LinearLayout>
在这里,我通过给它们android:layout_weight="1"
和android:layout_width="0dp"
赋予了LinearLayout内部的TextView相同的权重.
Here I've given equal weight to the TextViews inside the LinearLayout, by giving them android:layout_weight="1"
and android:layout_width="0dp"
.
然后通过提供android:gravity="right"
来确保布局内的文本朝着视图最右边缘的末端对齐.
And then by giving android:gravity="right"
ensured that the text inside the layout is aligned towards the end of the right-most edge of the view.
很高兴为您提供帮助,您可以询问是否有任何疑问.
Happy to help, you can ask if you have any queries.
这篇关于如何将文本对齐方式调整到textview的两个角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!