Android TextView 文本不会居中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10014819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:11:26  来源:igfitidea点击:

Android TextView text won't center

androidlayouttextview

提问by MrGibbage

I am trying to make a PIN entry activity, but one little thing is not quite working for me. I can't get the text to center in the TextView's at the top of the screenshot here:

我正在尝试进行 PIN 输入活动,但有一件小事对我来说不太奏效。我无法将文本置于屏幕截图顶部的 TextView 中心:

Pin Screenshot

引脚截图

The way it will work is when the user is entering a PIN, I will place a star in each TextView for visible feedback. The problem is, I want the star to be centered. I have tried layout_gravity="center", but it doesn't make a difference. Here's my current layout:

它的工作方式是当用户输入 PIN 时,我将在每个 TextView 中放置一个星号以获取可见反馈。问题是,我希望星星居中。我试过 layout_gravity="center",但没有什么区别。这是我目前的布局:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
     <TextView 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView1" 
     android:text="PIN Required" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginBottom="12sp"
     ></TextView>
     <LinearLayout 
     android:layout_width="match_parent" 
     android:id="@+id/linearLayout1" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="12sp" 
     android:layout_gravity="center_horizontal"
     >
         <TextView 
       android:id="@+id/text1" 
       android:layout_height="32sp" 
       android:layout_width="48sp" 
       android:layout_weight="1" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="48sp" 
       android:background="#FFFFFF" 
       android:text="X" 
       android:layout_gravity="center"
       ></TextView>
         <TextView 
       android:id="@+id/text2" 
       android:layout_height="32sp" 
       android:layout_width="48sp" 
       android:layout_weight="1" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="2sp" 
       android:background="#FFFFFF"
       ></TextView>
         <TextView 
       android:id="@+id/text3" 
       android:layout_height="32sp" 
       android:layout_width="48sp" 
       android:layout_weight="1" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="2sp" 
       android:background="#FFFFFF"
       ></TextView>
         <TextView 
       android:id="@+id/text4" 
       android:layout_height="32sp" 
       android:layout_width="48sp" 
       android:layout_weight="1" 
       android:layout_marginRight="48sp" 
       android:layout_marginLeft="2sp" 
       android:background="#FFFFFF"
       ></TextView>
     </LinearLayout>
     <LinearLayout 
     android:layout_width="match_parent" 
     android:id="@+id/linearLayout2" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="12sp"
     >
         <Button 
       android:text="1" 
       android:textColor="#FFFFFF" 
       android:id="@+id/button1" 
       android:layout_height="32sp" 
       android:layout_width="64sp" 
       android:layout_weight="1" 
       android:editable="false" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="48sp" 
       android:background="@drawable/action_background_gradient" 
       android:onClick="ButtonClicked"
       ></Button>
         <Button 
       android:text="2" 
       android:textColor="#FFFFFF" 
       android:id="@+id/button2" 
       android:layout_height="32sp" 
       android:layout_width="64sp" 
       android:layout_weight="1" 
       android:editable="false" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="2sp" 
       android:background="@drawable/action_background_gradient" 
       android:onClick="ButtonClicked"
       ></Button>
         <Button 
       android:text="3" 
       android:textColor="#FFFFFF" 
       android:id="@+id/button3" 
       android:layout_height="32sp" 
       android:layout_width="64sp" 
       android:layout_weight="1" 
       android:editable="false" 
       android:layout_marginRight="48sp" 
       android:layout_marginLeft="2sp" 
       android:background="@drawable/action_background_gradient" 
       android:onClick="ButtonClicked"
       ></Button>
     </LinearLayout>

     <!--... and so on for the rest of the buttons ... -->
 </LinearLayout>

I've got that android:text="X" in there for the first textview just so I can see if it is working. When I am done tweaking the layout, that will go away. What simple thing am I missing?

我在第一个 textview 中有那个 android:text="X" ,这样我就可以看看它是否正常工作。当我完成对布局的调整后,这种情况就会消失。我错过了什么简单的事情?

回答by josephus

use android:gravity="center"

android:gravity="center"

although I'm not seeing any EditText element in your code. You sure you posted the right code?

尽管我在您的代码中没有看到任何 EditText 元素。你确定你发布了正确的代码?

回答by Graeme

<TextView 
   android:id="@+id/text1" 
   android:layout_height="32sp" 
   android:layout_width="48sp" 
   android:layout_weight="1" 
   android:layout_marginRight="2sp" 
   android:layout_marginLeft="48sp" 
   android:background="#FFFFFF" 
   android:text="X" 
   android:layout_gravity="center"
   android:gravity="center"
/>

android:layout_gravity="center"relates to this View's position in it's parent. android:gravity="center"relates to where this View's contents are positioned.

android:layout_gravity="center"与此 View 在其父级中的位置相关。 android:gravity="center"与此 View 的内容所在的位置有关。

回答by Nitish

Instead of android:layout_gravity in text view use android:gravity="center_horizontal"

代替 android:layout_gravity 在文本视图中使用 android:gravity="center_horizontal"

回答by abriggs

This can be done programmatically as well, of-course:

这也可以以编程方式完成,当然:

textView.setGravity(Gravity.CENTER);

回答by Aleks G

To centre the text, use android:gravityon the TextView, not layout_gravity:

要使文本居中,请android:gravity在 TextView 上使用,而不是 layout_gravity:

<TextView 
   android:id="@+id/text1" 
   android:layout_height="32sp" 
   android:layout_width="48sp" 
   android:layout_weight="1" 
   android:layout_marginRight="2sp" 
   android:layout_marginLeft="48sp" 
   android:background="#FFFFFF" 
   android:text="X" 
   android:gravity="center" />