我知道这听起来很简单。我需要将一个文本放在中间,但是当文本太长时,它需要移到下面,但仍然要在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:我的代码更大;它在RelativeLayout中。
发布于 2011-09-07 05:08:43
同时将TextView
中的android:gravity
参数设置为center
。
为了测试不同布局参数的效果,我建议对每个元素使用不同的背景色,这样你就可以看到布局是如何随着重力、layout_gravity等参数的变化而变化的。
发布于 2011-09-07 12:21:55
使用这种方式
txt.setGravity(Gravity.CENTER);
发布于 2015-04-09 14:07:34
在xml中使用这种方式
<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time is precious, so love now."
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
/>
https://stackoverflow.com/questions/7326155
复制相似问题