【问题标题】:Set text gravity to center将文本重心设置为中心
【发布时间】:2023-04-08 10:06:02
【问题描述】:

将文本重力设置为中心时出现问题,它没有出现在中间?!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/AppBaseTheme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Large Text Large Text Large Text Large Text Large Text Large Text"
    android:layout_gravity="center"  
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

【问题讨论】:

  • this线程
  • 您必须发布整个布局 xml。

标签: android text textview center gravity


【解决方案1】:

我认为它只是在 IDE 中看起来那样尝试运行您的应用程序并进行测试。 我也发生过,但结果证明是 IDE 问题,所以请检查一下。

【讨论】:

    【解决方案2】:

    如果您希望整个 TextView 居中(这将使文本居中),请使用 android:layout_gravity

     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Large Text Large Text Large Text Large Text Large Text Large Text"
        android:layout_gravity="center"   // Here
        android:textAppearance="?android:attr/textAppearanceLarge" />
    

    如果这不能解决问题,请发布完整的 xml,因为您可能有其他 Views/properties 导致问题。

    【讨论】:

    • 它不能解决我更新我的问题的问题
    • 你确定这是完整的xml吗?因为如果是这样,我认为没有理由将该文本移到右侧。检查 IDE 的 GUI 并查看 TextView 的位置。另外,请尝试清理和重建您的项目。
    • 您还有其他我们看不到的事情正在发生。我只是将它复制到我的编辑器中,它显示正确。您是否按照我的建议尝试清理项目? AppBaseTheme 是做什么的?
    • 谢谢你我不小心从清单中的 中删除了 android:theme="@style/AppBaseTheme" ^_^ 现在可以使用了
    【解决方案3】:

    使用宽度来填充父级的其余部分

    android:layout_width="fill_parent"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2012-08-07
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多