Android:在左侧对齐文本,并将文本视图居中显示在其父视图中

Android:在左侧对齐文本,并将文本视图居中显示在其父视图中,android,text,textview,gravity,Android,Text,Textview,Gravity,你好,我面临一个小问题 我用重力左对齐视图左侧的文本,但我想在文本视图内居中,但也要在左侧对齐 以下是我现在拥有的: ___________________________________ _ _ _ _ _ _| aaaaaaaaaaaaa |_ _ _ _ _ _ _ _ _ _ _ _| aaaaaaaa |_ _ _ _ _ _ _ _ _ _ _ _| aaaaaaaaaa

你好,我面临一个小问题

我用重力左对齐视图左侧的文本,但我想在文本视图内居中,但也要在左侧对齐

以下是我现在拥有的:

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

            ___________________________________
_ _ _ _ _ _| aaaaaaaaaaaaa                     |_ _ _ _ _ _
_ _ _ _ _ _| aaaaaaaa                          |_ _ _ _ _ _
_ _ _ _ _ _| aaaaaaaaaaaaaa                    |_ _ _ _ _ _
            -----------------------------------
            ___________________________________
_ _ _ _ _ _|           aaaaaaaaaaa             |_ _ _ _ _ _
_ _ _ _ _ _|           aaaaaaaa                |_ _ _ _ _ _
_ _ _ _ _ _|           aaaaaaaaaaaaaa          |_ _ _ _ _ _
            -----------------------------------
其中:

            ___________________________________
_ _ _ _ _ _| aaaaaaaaaaaaa                     |_ _ _ _ _ _
_ _ _ _ _ _| aaaaaaaa                          |_ _ _ _ _ _
_ _ _ _ _ _| aaaaaaaaaaaaaa                    |_ _ _ _ _ _
            -----------------------------------
            ___________________________________
_ _ _ _ _ _|           aaaaaaaaaaa             |_ _ _ _ _ _
_ _ _ _ _ _|           aaaaaaaa                |_ _ _ _ _ _
_ _ _ _ _ _|           aaaaaaaaaaaaaa          |_ _ _ _ _ _
            -----------------------------------
_在文本视图之外

|=文本视图边缘

我试过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"/>

我所做的(文本左对齐但不居中)


我想要的(文本左对齐和居中)(我手动添加填充以获得渲染效果,但它脏了,无法适应所有文本):

只需将Textview父对象设置为:

android:gravity = "center"
只需将Textview设置为:

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

在文本视图中更改以下内容:

 android:gravity = "center|left|left"


问题在于textview宽度,它只是将自身环绕文本,并将其重心设置为居中,使其在环绕区域居中

更改宽度以匹配父对象和重心。这可能对你有帮助

android:layout_width="match_parent"
android:gravity = "center"
另一种方法:由于边界线与文本视图分离的中心对齐。您只需在文本视图中添加以下行

  android:layout_centerHorizontal="true"

如果您更改textview宽度以匹配父项或指定一些固定宽度,您将获得所需的内容:

<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"/>


请将RelativeLayout更改为LinearLayout make gravity and layout_gravity center

用于从左侧开始文本并以重力为中心。 请像这样使用重力性质

android:gravity="center|start"

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

注意文本视图的
layout\u width=“wrap\u 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>

显示xml请查看
文本视图的父级是什么??如果您可以发布包含
textview
textview
的父级的代码部分,则很容易解决您的问题。我将XML添加到我的主要帖子中。如果您的textview宽度为wrap content,则必须使用layoutGravity。如果textview宽度为matchParent,则仅使用重力。这将解决您的问题。@Foushi您是否在dp中为您的文本视图提供了宽度?您能准确地告诉我您想要的详细信息吗。所以我可以给你准确的密码。也给我完整的布局代码。看看我的主要帖子,我用更多的视觉更新了它,希望它能帮助你理解我的意思。让我们一起来。谢谢你的回答,但不幸的是,这不是我要找的,因为文本不会在左边对齐:我想要的:谢谢,这个答案帮了我。