我知道这听起来很容易。我需要将文本放在中心,但是当文本太长时,它需要放在下方,但仍然在我的 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:我的代码更大;它在一个相对布局中。
最佳答案
将TextView
中的android:gravity
参数设置为center
。
为了测试不同布局参数的效果,我建议为每个元素使用不同的背景颜色,这样您就可以看到您的布局如何随着重力、layout_gravity 等参数而变化。
关于java - 将文本中心与 android 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7326155/