textview中的中心文本正在根据文本长度更改视图大小

时间:2016-01-22 05:40:58

标签: android android-layout

我正在尝试使用居中文本创建大小相同的框。我遇到了奇怪的问题。当我创建没有gravity.center的盒子时,我得到以下

enter image description here

当我添加属性中心时,我得到以下

enter image description here

文本很好地居中,但是当文本大小发生变化时,视图会因某种原因而移位。即使我将文字缩减为一个单词,那么该框比其他所有内容更大

这是我的布局..这是怎么回事?我该如何解决?

   <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
           <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="Name of Player Added" />

          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/bg"
             android:layout_gravity="top"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="Name of Player Added" />           
          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="blah blah blah blah blah blah blah blah blah blah" /> 
          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text=" blah blah blah blah blah blah blah blah blah blah" /> 

        </LinearLayout>   

3 个答案:

答案 0 :(得分:4)

我刚刚在LinearLayout中添加了android:gravity =“center”。试试这段代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:color/black"
android:gravity="center">
<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@android:color/holo_blue_bright"
    android:layout_marginRight="7dp"
    android:text="Name of Player Added"
    android:gravity="center"/>

<TextView
    android:id="@+id/tvPlayer2"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@android:color/holo_red_light"
    android:layout_marginRight="7dp"
    android:text="Name of Player Added"
    android:gravity="center"/>
<TextView
    android:id="@+id/tvPlayer3"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@android:color/darker_gray"
    android:layout_marginRight="7dp"
    android:gravity="center"
    android:text="blah blah blah blah blah blah blah blah blah blah" />
<TextView
    android:id="@+id/tvPlayer4"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@android:color/holo_green_dark"
    android:layout_marginRight="7dp"
    android:text=" blah blah blah blah blah blah blah blah blah blah"
    android:gravity="center"/>

</LinearLayout>

答案 1 :(得分:0)

尝试下面的布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="4">

<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:text="Name of Player Added"
    android:layout_weight="1"
    android:padding="5dp"
    android:gravity="center"
    />

<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:text="Name of Player Added"
    android:layout_weight="1"
    android:padding="5dp"
    android:gravity="center" />
<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:text="blah blah blah blah blah blah blah blah blah blah"
    android:layout_weight="1"
    android:padding="5dp"
    android:gravity="center" />
<TextView
    android:id="@+id/tvPlayer1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:text=" blah blah blah blah blah blah blah blah blah blah"
    android:layout_weight="1"
    android:gravity="center"/>

</LinearLayout>

答案 2 :(得分:0)

试试这样。希望你能得到你想要的输出。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:orientation="horizontal">
           <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="match_parent"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="Name of Player Added" />

          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="match_parent"
             android:background="@drawable/bg"
             android:layout_gravity="top"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="Name of Player Added" />           
          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="match_parent"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text="blah blah blah blah blah blah blah blah blah blah" /> 
          <TextView
             android:id="@+id/tvPlayer1"
             android:layout_width="80dp"
             android:layout_height="match_parent"
             android:background="@drawable/bg"
             android:padding="5dp"
             android:gravity="center"
             android:layout_marginRight="7dp"
             android:text=" blah blah blah blah blah blah blah blah blah blah" /> 

        </LinearLayout>