【问题标题】:Android align text top centerAndroid 将文本顶部居中对齐
【发布时间】:2014-09-01 06:31:03
【问题描述】:

如果这是重复的,我很抱歉,但我找不到任何我正在寻找的东西。基本上,我想将文本视图中的文本对齐到顶部中心。如果可能的话,我想用 XML 来做这件事。所以我想结合

android:gravity="center"

android:gravity="top"

使其与两者对齐。我已经尝试将这两个属性都放在文本视图元素中,但它只是给出了一个错误。

【问题讨论】:

  • 请发布错误和所有其他相关信息

标签: android gravity


【解决方案1】:

首先,不能复制属性,但可以组合值。

android:gravity="g1|g2"

然而,center 暗示 垂直和水平居中 所以top|center 不正确。你应该使用

android:gravity="top|center_horizontal"

【讨论】:

  • 太棒了。非常感谢。
  • 但是当我尝试使用 android studio 时。只是“顶部|中心”也很好。猜猜它们对 TextView 的实现有同样的影响。
【解决方案2】:

如果你想对重力应用多个属性,你必须像这样使用|

android:gravity="top|center"

【讨论】:

    【解决方案3】:

    使用 RelativeLayout 作为您的 ViewGroup 并执行以下操作:

     <?xml version="1.0" encoding="utf-8" ?>
     <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_height="match_parent"
         android:layout_width="match_parent">
         <TextView
            android:id="@+id/textViewDemo"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"/>
    
      </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      相关资源
      最近更新 更多