【问题标题】:How to make the text center vertical in a line in TextView of Android如何在Android的TextView中使文本中心在一行中垂直
【发布时间】:2014-05-02 23:09:00
【问题描述】:

我有一个带有 100dp lineSpacingExtra多行编辑文本。

现在文本显示在一行的顶部。并且光标的高度和行高一样,比文字的高度高很多。

如何使文本显示在行的中心? 或者,如果我可以在一行中调整文本的顶部填充或底部填充。

注意: 我的意思是不使用 android:gravity="center_vertical" 使文本中心在 TextView 中垂直。 我想在行中垂直显示文本中心。

xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@android:color/transparent"
        android:fadingEdge="vertical"
        android:gravity="top"
        android:inputType="textCapSentences|textMultiLine|textShortMessage"
        android:lineSpacingExtra="100dp"
        android:longClickable="false"
        android:minLines="10"
        android:paddingBottom="5dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="5dp"
        android:scrollbars="vertical"
        android:text="Text ABCDE\n Line2"
        android:textAlignment="center"
        android:textSize="16sp" />
</LinearLayout>

感谢任何帮助。 谢了!

【问题讨论】:

  • 在此处显示您的 xml 和快照。
  • 只用EditText是没有办法的,你应该创建一个LinearLayout或RelativeLayout来环绕EditText,将LinearLayout或RelativeLayout的重力设置为Gravity.Center,将EditText的高度设置为Wrap_Content,它应该工作

标签: android textview android-edittext line center


【解决方案1】:

在此处附上我的结果:
无法在应用程序中执行此操作。
因为 TextView 在框架中的一行顶部绘制文本。
如果你想要显示的文本在一行中垂直居中的视觉效果,必须修改框架代码。

文件位置是:
frameworks/base/core/java/android/text/StaticLayout.java

变化:

-        lines[off + DESCENT] = below + extra;
+        lines[off + DESCENT] = below + extra/2;//Show the text center vertically in the line.

如果有问题或有更好的方法,请纠正我。

【讨论】:

    【解决方案2】:

    编辑答案:

    输出是:

    代码(来自我自己的 xml 标签,所以更改你的 id):

            <LinearLayout
                    android:id="@+id/layout_feedback"
                    android:layout_width="401dp"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tv_six"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="27dp"
                    android:background="@drawable/textfield_activated_holo_dark"
                    android:gravity="center_horizontal"
                    android:orientation="horizontal" >
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:gravity="center_horizontal"
                        android:hint="Em   "
                        android:visibility="invisible"
                        android:textColor="@color/white"
                        android:textSize="24sp" />
    
                    <EditText
                        android:id="@+id/ed_feedback_know"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/textfield_empty_holo_dark"
                        android:focusable="true"
                        android:text="Text ABCDE\n Line2"
                        android:focusableInTouchMode="true"
                        android:inputType="textEmailAddress"
                        android:textColor="@android:color/black"
                        android:textSize="24sp" />
                </LinearLayout>
    

    【讨论】:

    • 感谢您的回复。但我需要的是在其行的中心显示文本,而不是在 TextView 的中心显示文本。
    • 嘿 WJ S,感谢您指出确切的问题,我已经编辑了我的答案。希望对你有帮助
    • 对不起,我错过了强调编辑文本是多行的,而 lineSpacingExtra 是 100dp。如果这样做,很容易发现文本显示在行的顶部。我想用大 lineSpacingExtras 将文本放在行的中心。无论如何,感谢您的热情帮助。 :)
    猜你喜欢
    • 2020-11-23
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2014-03-23
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多