【问题标题】:Android: Centering text in a textview when texview is centered in a layoutAndroid:当texview在布局中居中时,在textview中居中文本
【发布时间】:2013-12-27 20:45:35
【问题描述】:

我需要文本位于屏幕的直接中心,并希望 textview 填充父级。但它一直将文本的开头对齐到屏幕的中心。

在android中使用textview时如何使文本居中?

这就是我所拥有的

这是xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"  //removing this line has no effect
        android:gravity="center"
        android:text="TextView" />
</RelativeLayout>

-------------------------------------

【问题讨论】:

  • PS:请注意,我确实使用了android:gravity="center",所以请不要将其称为任何给出答案的内容

标签: java android xml textview android-relativelayout


【解决方案1】:

如果您必须在此处使用RelativeLayout 您的解决方案:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="TextView" />

</RelativeLayout>

如果需要,您也可以使用LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="TextView" />

</LinearLayout>

【讨论】:

  • 请看我的第二张照片,看看你第一个答案的复制粘贴结果
【解决方案2】:

试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="TextView" />

</RelativeLayout>

或者

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="center">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="TextView" />

</RelativeLayout>

【讨论】:

    【解决方案3】:

    我认为您需要首先保存文件 activity_main.xml* 以查看未保存的更改(CTRL + S),其次布局是它在 Nexus 上的外观,您最好创建单独的布局文件夹600dp+。第三,我可以在您的控制台上看到一些错误,请消除它,如果没有任何效果,请清理您的项目并重新启动您的 Eclipse,我想它会起作用

    【讨论】:

      猜你喜欢
      • 2012-04-10
      • 2014-07-17
      • 1970-01-01
      • 2015-02-04
      • 2011-02-16
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多