gpt4 book ai didi

android - TextView 字段中换行后的文本居中

转载 作者:行者123 更新时间:2023-11-29 01:53:48 28 4
gpt4 key购买 nike

我一直在做这方面的功课,但我遇到的所有建议似乎都没有用。我是 Android 应用程序的新手,虽然答案对某些人来说可能很简单,但我就是看不出我哪里出错了....

我有一个 TextView 字段,其中包含一个带换行符 (\n) 的字符串值。在图形输出中,文本的下行没有以 TextView 容器为中心。我的代码如下:

<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=".MainActivity" >

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:text="@string/maxText"
android:textAlignment="gravity"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />

</RelativeLayout>

我的字符串如下:

<string name="maxText">My text part 1:\nMy text part 2</string>

输出文本的下行与容器左对齐,但我要求它在中心。我已经尝试了我在论坛等中遇到的所有建议 - 但无济于事。

如有任何帮助,我们将不胜感激。

最佳答案

实际上我们不需要把那个文本放到两个 TextView 中。

在 XML 文件中为包含 TextView 的布局设置 android:layout_gravity="center"

如果这不起作用。

尝试为布局参数设置重心,并以编程方式将此参数设置为布局。如下所示。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
yourLayout.setLayoutParams(params);
yourLayout.setGravity(Gravity.CENTER);

这对我有用。

希望这对您也适用。 :)

关于android - TextView 字段中换行后的文本居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16506099/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com