简体   繁体   English

TextView的文本对齐方式

[英]TextView's text alignment

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    android:id="@+id/widget33"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:stretchColumns="*"
    xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
    android:id="@+id/widget34"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<TextView
    android:id="@+id/widget35"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:gravity="center" />
<TextView
    android:id="@+id/widget36"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:gravity="center" />
<TextView
    android:id="@+id/widget37"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:gravity="center" />
</TableRow>
</TableLayout>

The text in TextViews is not aligned to the centre even though the gravity is set center .Text is shifted to extreme right.When I check the same code in droidDraw every thing seems fine.Whats happening? 即使将gravity设置为centerTextViews的文本也不会与centre对齐。文本会移到最右端。当我在droidDraw中检查相同的代码时,一切似乎都很好,这是怎么回事?

只要您为android:layout_widthandroid:layout_height使用wrap_content ,重力就不会做任何事情。

//尝试

android:layout_gravity="center" 

If you set TextView to wrap_content it takes up as small space as posible. 如果将TextView设置为wrap_content,则它将占用尽可能小的空间。 When you set it to center it centers inside the wrap_content. 当您将其设置为居中时,它将居在wrap_content内部。 This makes it seem that there is no difference. 这似乎没有什么区别。

If you set TextView to fill or match_parent it will center inside the TableLayout. 如果将TextView设置为fill或match_parent,它将位于TableLayout内部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM