【问题标题】:Android TextView text won't centerAndroid TextView 文本不会居中
【发布时间】:2012-04-18 09:06:25
【问题描述】:

我正在尝试进行 PIN 输入活动,但有一点不太适合我。我无法让文本在屏幕截图顶部的 TextView 中居中:

它的工作方式是当用户输入 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>

我已经为第一个文本视图添加了 android:text="X" ,这样我就可以查看它是否正常工作。当我完成调整布局时,它就会消失。 我错过了什么简单的事情?

【问题讨论】:

    标签: android layout textview


    【解决方案1】:

    使用android:gravity="center"

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

    【讨论】:

    • 那是因为我是个傻瓜,我想说的是TextView。同时做两件事。但我确实有gravity="center",但它不起作用。
    • 噢!我有 layout_gravity,而不仅仅是重力。叹息...谢谢!
    • 谢谢,这对我帮助很大。
    • 啊啊!我会犯多少次同样的错误!我应该在额头纹上layout_^H^H^H^H^H^H^Hgravity。
    【解决方案2】:
    <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" 与此视图在其父视图中的位置相关。 android:gravity="center" 与此视图内容的位置有关。

    【讨论】:

    • 对于我的特定活动,将android:gravity单独设置为center并不能解决问题,我还需要将android:layout_gravity设置为center +1
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      要使文本居中,请在 TextView 上使用 android:gravity,而不是 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" />
      

      【讨论】:

        【解决方案5】:

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

        textView.setGravity(Gravity.CENTER);
        

        【讨论】:

          猜你喜欢
          • 2014-02-14
          • 1970-01-01
          • 1970-01-01
          • 2015-01-15
          • 1970-01-01
          • 2023-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多