Android: 如何将文本左对齐并使TextView在其父视图中居中显示?

17
Hello,我希望你能帮我解决一个小问题。
我正在使用GRAVITY LEFT来使我的文本对齐到视图的左边,但我想让它在TextView中居中对齐,同时也要向左对齐。
以下是我目前的情况:
            ___________________________________
_ _ _ _ _ _| aaaaaaaaaaaaa                     |_ _ _ _ _ _
_ _ _ _ _ _| aaaaaaaa                          |_ _ _ _ _ _
_ _ _ _ _ _| aaaaaaaaaaaaaa                    |_ _ _ _ _ _
            -----------------------------------

我想要的是:
            ___________________________________
_ _ _ _ _ _|           aaaaaaaaaaa             |_ _ _ _ _ _
_ _ _ _ _ _|           aaaaaaaa                |_ _ _ _ _ _
_ _ _ _ _ _|           aaaaaaaaaaaaaa          |_ _ _ _ _ _
            -----------------------------------

在哪里:

_ _ _ _ = 文本视图之外

| = TextView 边缘

我尝试了 android:gravity = "left|center",但它不起作用,有什么想法吗?

谢谢。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/image_button_wrapper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_centerVertical="true">

    <ImageView
        android:id="@+id/left_button_image"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/spacing_normal"/>

    <Textview
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/left_button_image"
        android:gravity = "center|left|left"
        android:clickable="false"
        android:focusable="false"
        app:font_name="Roboto-Light.ttf"/>

我想要的是(左对齐且居中)(我手动添加了填充以获得渲染,但这很不好看并且不能适应所有文本):输入图像描述

我已经拥有的是(左对齐但未居中)输入图像描述


2
请显示XML。 - Tim
textView 的父级是什么?如果您可以发布包含 textview 和其父级的代码部分,将更容易解决您的问题。 - himanshu1496
我在我的主贴中添加了XML。 - Foushi
如果你的TextView宽度是wrap content,那么你必须使用layoutGravity。而如果你的TextView宽度是matchParent,则仅使用gravity即可。这将解决你的问题。 - Ankush Bist
@Foushi 你在TextView中使用了dp来设置宽度吗? - Dhruvi
简单地拿一个LinearLayout,然后将你的TextView移动到该LinearLayout中,接着为LinearLayout设置android:gravity="center",为TextView设置android:gravity="left"。 - Kaushal Kishor
7个回答

11

只需将Textview的父级设置为:

android:gravity = "center"

只需将Textview设为:

android:gravity = "center_vertical|left|start"
android:layout_margin="20dp"

你能详细说明你需要什么吗?这样我才能给你准确的代码。同时,请提供布局的完整代码。 - Ashish Vora
看一下我的主要帖子,我已经更新了更多的可视化内容,希望可以帮助您理解我的意思。 - Foushi
让我们在聊天中继续这个讨论 - Ashish Vora

2

问题在于您的文本视图宽度,它只是围绕文本自动换行,并将其重力设置为中心,只会将其居中在其包裹区域中。

将宽度更改为match-parent并将重力更改为中心。这可能会对您有所帮助。

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

另一种方法:由于边界线是与文本视图分开的中心对齐的,因此您只需在您的文本视图中添加以下行。

  android:layout_centerHorizontal="true"

2

如果你将TextView的宽度设置为match_parent或者固定一个宽度,就可以得到你想要的效果:

<TextView
    android:layout_width="100dp"
    //android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hi! This is amazing!!"
    android:gravity="center_horizontal"/>

1

要从左侧开始并使文本居中,请使用如下的gravity属性。

android:gravity="center|start"

1
在你的TextView中更改以下内容:
 android:gravity = "center|left|left"

to

 android:gravity = "center"

谢谢您的回答,但不幸的是这并不是我要找的,因为文本不会左对齐:https://i.gyazo.com/01916284114cc21fa3f396e8c7509fd9.png 我想要的是:http://i.stack.imgur.com/V0QYO.png - Foushi

0
请将RelativeLayout更改为LinearLayout,并使gravity和layout_gravity居中。

0

我正在使用constraintLayout,这对我很有效。

请注意textView的layout_width ="wrap_content"

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">


        <TextView
            android:id="@+id/headerTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|center_vertical"
            android:text="Sign Up"
            android:textSize="25sp"
            app:layout_constraintBottom_toTopOf="@id/subHeaderTitle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/subHeaderTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.Apple\n2.Orange\n3.Pear"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接