Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果文本视图有1行或2行,如何设置文本视图高度以包装内容?_Android_Android Layout_Textview_Android Constraintlayout - Fatal编程技术网

Android 如果文本视图有1行或2行,如何设置文本视图高度以包装内容?

Android 如果文本视图有1行或2行,如何设置文本视图高度以包装内容?,android,android-layout,textview,android-constraintlayout,Android,Android Layout,Textview,Android Constraintlayout,在这件事上,我比我想象的还要努力,这是我的问题 如下图所示,我希望我的TextView在父视图中处于中心水平,但也要将垂直中心与左侧视图对齐 问题来自TextView,文本可能更长,我希望它位于属性为maxLength=12的两行上 我的XML: <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="0dp" android:layout_height=&quo

在这件事上,我比我想象的还要努力,这是我的问题

如下图所示,我希望我的
TextView
父视图
中处于
中心
水平,但也要将
垂直
中心与左侧视图对齐

问题来自
TextView
,文本可能更长,我希望它位于属性为
maxLength=12
的两行上

我的XML:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintEnd_toStartOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/roundTextView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/oval"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/descriptionTextView"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/descriptionTextView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:maxLength="12"
        android:maxLines="2"
        android:text="Long Text 2 Lines"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="@+id/roundTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/roundTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>

但是,如果我改为一个小文本,由于
0dp
高度
的原因,结果与预期不符。如果我把
wrap_content
,则两行文字不起作用

处理这种情况的正确方法是什么?

添加:

android:gravity="center_vertical"
android:maxLines="2"

文本总是垂直显示在中间,不需要<代码> Android:文本地址=“Center”< /C> > /P>这是正确的答案,谢谢。你确定文本对齐吗?我收到一条警告,但文字没有“居中”。我会在5分钟内接受这个答案。是的,我肯定。如果您也希望在中间水平显示文本,请改用:

android:gravity=“center”
。添加包裹内容时会发生什么情况?