sti*_*ike 50

我只能看到textAlignment是View Class的成员,而gravity是TextView类的成员.因此,对于TextView及其子类,您可以使用重力,同时可以对所有视图使用textAlignment.

由于TextView及其子类需要更多的文本对齐功能,因此您可以看到有更多的重力选项,其中textAlignment只有基本选项.虽然这只是我的猜测,因为我没有找到任何有关差异的明确文件.

您可以看到这两个文档链接:textAlignmentgravity.


laa*_*lto 12

还有一点没有明确提到:

如果您的应用程序android:supportsRtl="false"在清单application标签中禁用了 RTL 支持,则View textAlignment在工作时不适用于文本定位TextView gravity

另一个喜欢gravity.


小智 10

使用API​​ 15,android:textAlignment可能没有所需的结果.下面的代码段尝试TextView使用第一个对象居中android:textAlignment="center".第二个用途android:gravity="center_horizontal".的textAlignment,而重力工作正常,没有任何效果.使用API​​ 17+,可以textAlignment按预期使文本居中.

为了确保您的文本与所有版本正确对齐,我会选择重力.

<LinearLayout
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Fri"
        android:textAlignment="center"
        android:textSize="16sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="29"
        android:gravity="center_horizontal"
        android:textSize="18sp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

API 15中的结果布局:
API 15中的最终布局

API 17+中的最终布局:
API 17+中的最终布局

  • `android:textAlignment` 仅适用于 API 17+,这就是为什么对较低版本没有影响的原因。 (2认同)

小智 6

据我所见,textAlignment似乎大部分没有使用。通过它的描述,它应该只进行左右对齐。重力似乎是对textAlignment的一种改进。