如何在TextView自己的容器内居中放置文本?

时间:2018-10-31 08:39:36

标签: android text textview android-gravity

我需要创建一个:

enter image description here

如您所见,这些行未与文本完全居中。

小写文本使问题变得更大。

文字偏低,不是居中显示

这是我的代码-容器是

的线性布局
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中的问题,问题和解决方案。

理解问题的最佳简便方法是小写和大写之间的区别...大写我没问题。

6 个答案:

答案 0 :(得分:1)

这是我针对类似要求所做的工作,效果很好。

    <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>

这是屏幕部分的快照。

enter image description here

因此android:layout_gravity="center"应该有效。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

我用过:

 android:layout_gravity="center_horizontal"

而不是:

  android:gravity="center_vertical"

对我有用。

答案 3 :(得分:0)

<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"/>

答案 4 :(得分:0)

我猜父级是水平线性布局。所以应该使用:

android:layout_gravity="center_vertical"

代替

android:gravity="center_vertical"

但是如果父母的身高是wrap_content,则结果应该相同。

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

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

答案 5 :(得分:0)

                <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>