与 android 对齐文本中心

发布于 2024-12-03 06:34:14 字数 674 浏览 3 评论 0原文

我知道这听起来很容易。我需要将文本放在中心,但是当文本太长时,它需要位于下方,但仍与 xml 的中心对齐。

这是我的代码:

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_centerHorizontal="true"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>

我放置了 paddingTop 和 Bottom 因为我需要一些空间。 PS:我的代码比较大;它位于相对布局中。

I know it sounds easy. I need to put a text in center, but when the text is too long it needs to go below, but still align in the center of my xml.

Here's my code :

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_centerHorizontal="true"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>

I put paddingTop and Bottom because I need some space.
PS: My code is bigger; it's in a RelativeLayout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

霞映澄塘 2024-12-10 06:34:14

TextView 中的 android:gravity 参数设置为 center

为了测试不同布局参数的效果,我建议为每个元素使用不同的背景颜色,这样您就可以看到布局如何随着重力、layout_gravity 或其他参数的变化而变化。

Set also android:gravity parameter in TextView to center.

For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others.

谁把谁当真 2024-12-10 06:34:14

用这种方式

txt.setGravity(Gravity.CENTER);

use this way

txt.setGravity(Gravity.CENTER);
只是我以为 2024-12-10 06:34:14

在xml中使用这种方式

   <TextView
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time is precious, so love now."
        android:gravity="center"
        android:textSize="30dp"
        android:textColor="#fff"
        />

use this way in xml

   <TextView
        android:id="@+id/myText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time is precious, so love now."
        android:gravity="center"
        android:textSize="30dp"
        android:textColor="#fff"
        />
2024-12-10 06:34:14

您可以使用以下内容:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

 <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Your text"
        android:typeface="serif" />
</LinearLayout>

布局需要是相对的才能使用“center”属性。

You can use the following:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

 <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Your text"
        android:typeface="serif" />
</LinearLayout>

The layout needs to be relative for the "center" property to be used.

固执像三岁 2024-12-10 06:34:14

在 TextView 中添加 android:gravity="center" 就可以了(父布局是 Relative/Linear)!

另外,您应该避免使用 dp 作为字体大小。使用 sp 代替。

Adding android:gravity="center" in your TextView will do the trick (be the parent layout is Relative/Linear)!

Also, you should avoid using dp for font size. Use sp instead.

不必你懂 2024-12-10 06:34:14

只需添加这两行即可;

android:gravity="center_horizontal"
android:textAlignment="center"

Just add these 2 lines;

android:gravity="center_horizontal"
android:textAlignment="center"
梦明 2024-12-10 06:34:14

在 TextView 上添加布局重力和重力以及中心值

<TextView
    android:text="welcome text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    />

add layout_gravity and gravity with center value on TextView

<TextView
    android:text="welcome text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    />
难理解 2024-12-10 06:34:14
android:layout_gravity="center"

或者

android:gravity="center"

两者都适合我。

android:layout_gravity="center"

or

android:gravity="center"

both works for me.

如果没结果 2024-12-10 06:34:14

或者检查一下,这将有助于一次对齐所有元素。

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>

Or check this out this will help align all the elements at once.

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showdescriptioncontenttitle"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
>
    <TextView 
        android:id="@+id/showdescriptiontitle"
        android:text="Title"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>
爱人如己 2024-12-10 06:34:14

如果您想更改 mainActivity.java 文件中的对齐方式,请使用重力。例如:

chatMsg.setGravity(Gravity.RIGHT);

chatMsg 是我的textView。我希望文本右对齐,并且这段代码运行良好。

If you want to change the alignment from the mainActivity.java file, then use the gravity. For example:

chatMsg.setGravity(Gravity.RIGHT);

chatMsg is my textView. I wanted the text to be right aligned and this code worked well.

吃素的狼 2024-12-10 06:34:14

您可以通过添加这些行轻松完成此操作

android:layout_width="match_parent"
android:gravity="center"
android:textAlignment="center"

you can easily done this by adding these lines

android:layout_width="match_parent"
android:gravity="center"
android:textAlignment="center"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文