Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中的文本/布局对齐(文本对齐,重力)_Android_Mobile_Layout_Text Alignment_Layout Gravity - Fatal编程技术网

Android中的文本/布局对齐(文本对齐,重力)

Android中的文本/布局对齐(文本对齐,重力),android,mobile,layout,text-alignment,layout-gravity,Android,Mobile,Layout,Text Alignment,Layout Gravity,android:textAlignment和android:gravity之间有什么区别?据我所知,textAlignment似乎大部分都没有使用过。 根据它的描述,它应该进行右对齐或左对齐。 Gravity似乎是一种改进的textAlignment。我所能看到的是textAlignment是View类的成员,Gravity是TextView类的成员。因此,对于TextView及其子类,您可以使用重力,同时可以对所有视图使用textAlignment 由于TextView及其子类需要更多的文本对

android:textAlignment
android:gravity
之间有什么区别?

据我所知,textAlignment似乎大部分都没有使用过。 根据它的描述,它应该进行右对齐或左对齐。
Gravity似乎是一种改进的textAlignment。

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

由于TextView及其子类需要更多的文本对齐功能,因此您可以看到gravity中有更多选项,而textAlignment中只有基本选项。虽然这只是我的猜测,因为我还没有找到任何关于差异的清晰文档


您可以看到以下两个文档链接:和。

对于API 15,
android:textAlignment
可能没有预期的结果。下面的代码段尝试使用android:textAlignment=“center”将第一个
TextView
对象居中。第二个使用android:gravity=“center\u 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>

API 15中的结果布局:

API 17+中的结果布局:

另一点没有明确提及:

如果您的应用程序在清单
应用程序
标记中禁用了RTL支持,例如
android:supportsRtl=“false”
,则
视图
文本对齐
不适用于文本定位,而
文本视图
重力
工作


另一个喜欢使用android的原因是:textAlignment仅适用于API 17+,这就是为什么对较低版本没有影响。使用其他人的小部件时要小心,重力似乎不会覆盖
textAlignment
,因此,例如,当覆盖
Snackbar
默认设置时,您需要设置
textAlignment
,而不是
gravity
。ie.
val textView=sb.view.findViewById(com.google.android.material.R.id.snackbar\u text)textView.textAlignment=view.text\u ALIGNMENT\u CENTER