首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将文本居中与android对齐

将文本居中与android对齐
EN

Stack Overflow用户
提问于 2011-09-07 05:04:15
回答 9查看 370.7K关注 0票数 163

我知道这听起来很简单。我需要将一个文本放在中间,但是当文本太长时,它需要移到下面,但仍然要在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中。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2011-09-07 05:08:43

同时将TextView中的android:gravity参数设置为center

为了测试不同布局参数的效果,我建议对每个元素使用不同的背景色,这样你就可以看到布局是如何随着重力、layout_gravity等参数的变化而变化的。

票数 416
EN

Stack Overflow用户

发布于 2011-09-07 12:21:55

使用这种方式

txt.setGravity(Gravity.CENTER);
票数 30
EN

Stack Overflow用户

发布于 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"
        />
票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7326155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档