Android TextView文本不会居中

MrG*_*age 8 layout android textview

我正在尝试进行PIN输入活动,但是一件小事对我来说并不适合.我无法将文本放在屏幕顶部的TextView中心:

Pin截图

它的工作方式是当用户输入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>
Run Code Online (Sandbox Code Playgroud)

我在第一个textview中有那个android:text ="X",所以我可以看看它是否正常工作.当我完成调整布局时,这将消失.我错过了什么简单的事情?

jos*_*hus 39

使用 android:gravity="center"

虽然我没有在你的代码中看到任何EditText元素.你确定你发布了正确的代码?

  • 卫生署!我有layout_gravity,而不仅仅是引力.叹了......谢谢! (7认同)
  • 啊哈!多少次我会犯同样的错误!! 我应该纹身布局_ ^ H ^ H ^ H ^ H ^ H ^ H ^ Hgravity在我的额头上. (2认同)

Gra*_*eme 13

<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"
/>
Run Code Online (Sandbox Code Playgroud)

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