【问题标题】:How to center text inside TextView own container?如何在 TextView 自己的容器中居中文本?
【发布时间】:2019-04-04 09:08:55
【问题描述】:

我需要创建这个:

正如你所看到的那样,这些线条并不完全以文本为中心。

小写文本的问题越来越大。

文字太低,不居中是自己的观点

这是我的代码 - 容器是带有

的线性布局
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:orientation="horizontal

<View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_marginRight="10dp"
    android:layout_weight="1"
    android:background="@color/gray_separator" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:text="How ?"
    android:textColor="@color/gray_login_or"
    android:textSize="26sp" />

<View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_marginLeft="10dp"
    android:layout_weight="1"
    android:background="@color/gray_separator" />

我尝试过使用相对布局,但这不是问题,TextView 中的问题和解决方案。

理解问题最好的简单方法是小写和大写之间的不同......在大写中我没有问题。

【问题讨论】:

  • 试试 android:gravity="center" 而不是 "c​​enter_vertical"
  • 没有。我试过了。我不相信这是一个重力问题。也许我错了。就像 AjustViewBounds 一样丢失了
  • 将所有 3 个视图放在一个线性布局中,然后让您的 textview 在高度上与父级匹配,然后使用重力进行文本对齐
  • 您粘贴了三个视图,它们的父级是什么?这个问题缺乏信息。

标签: android text textview android-gravity


【解决方案1】:

我用过:

 android:layout_gravity="center_horizontal"

而不是:

  android:gravity="center_vertical"

它对我有用。

【讨论】:

    【解决方案2】:
    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="@color/gray_separator"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="26sp"
        android:text="How ?"
        android:textColor="@color/gray_login_or"/>
    
    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="@color/gray_separator"/>
    

    【讨论】:

      【解决方案3】:

      这是我为类似要求所做的,效果很好。

          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="4"
              >
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="5dp"
                  android:src="@drawable/dashline"
                  android:layerType="software"
                  android:layout_below="@+id/title"
                  android:layout_marginLeft="8dp"
                  android:layout_marginRight="8dp"
                  android:layout_marginTop="0dp"
                  android:layout_marginBottom="0dp"
                  android:layout_weight="2"
                  android:layout_gravity="center"
                  />
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="OR"
                  android:textAllCaps="false"
                  android:textColor="@color/white"
                  android:textSize="25dp"
                  android:layout_margin="10dip"
                  android:layout_gravity="center"
                  android:clickable="true"
                  />
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="5dp"
                  android:src="@drawable/dashline"
                  android:layerType="software"
                  android:layout_below="@+id/title"
                  android:layout_marginLeft="8dp"
                  android:layout_marginRight="8dp"
                  android:layout_marginTop="0dp"
                  android:layout_marginBottom="0dp"
                  android:layout_gravity="center"
                  android:layout_weight="2"
                  />
      
          </LinearLayout>
      

      我正在根据需要使用 ImageView,您可以将它们替换为您想要的 View。

      这是我的dashline.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="line">
      
          <stroke
              android:color="#ffffff"
              android:dashWidth="8px"
              android:dashGap="0px"
              android:width="1dp"/>
      </shape>
      

      这是屏幕部分的快照。

      所以android:layout_gravity="center" 应该可以工作。

      【讨论】:

      • 如果您将 OR 更改为小写,您将看到问题,很好的例子
      【解决方案4】:

      在您的文本视图中使用android:gravity="center"

      【讨论】:

        【解决方案5】:

        我猜 parent 是一个水平线性布局。所以应该使用这个:

        android:layout_gravity="center_vertical"
        

        而不是

        android:gravity="center_vertical"
        

        但是如果父母的高度是wrap_content,结果应该是一样的。

        顺便说一句,字母字体不在空间的中间水平。它比中心线略低,对人眼来说更舒适。参考:

        https://en.wikipedia.org/wiki/File:Typography_Line_Terms.svg

        【讨论】:

          【解决方案6】:
                          <android.support.v7.widget.CardView
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:layout_margin="5dp">
          
          
                          <LinearLayout
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:layout_marginLeft="10dp"
                              android:layout_marginRight="10dp"
                              android:layout_marginTop="0dp"
                              android:orientation="horizontal">
          
                              <View
                                  android:layout_width="40dp"
                                  android:layout_height="1dp"
                                  android:background="@color/black"
                                  android:layout_gravity="center_vertical"
                                  android:layout_weight="1"/>
          
                              <TextView
                                  android:layout_gravity="center"
                                  android:gravity="center"
                                  android:id="@+id/Imagetxt"
                                  android:layout_width="20dp"
                                  android:layout_height="wrap_content"
                                  android:layout_weight="1"
                                  android:text="Image Cart"
                                  android:clickable="false"
                                  android:textColor="@color/orange"/>
          
                              <View
                                  android:layout_width="40dp"
                                  android:layout_height="1dp"
                                  android:background="@color/black"
                                  android:layout_gravity="center_vertical"
                                  android:layout_weight="1"/>
          
                      </LinearLayout>
          
          
                          </android.support.v7.widget.CardView>
          

          【讨论】:

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