简体   繁体   English

包装容器中包含多行文本的TextView-错误的高度?

[英]TextView with multiple lines of text in a wrapping container - wrong height?

I found the following problem. 我发现以下问题。 Having something simple like this works: 像这样简单的东西可以工作:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="long text taking multiple lines ..."/>

</LinearLayout>

But if you have another component in the layout the TextView starts to behave weird. 但是,如果您在布局中还有另一个组件,则TextView会开始表现异常。 It seems to be one line short having text running out of the frame of the container: 文本超出容器框架似乎很短:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="18sp"
    android:text="long text over multiple lines ..."/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Bla"/>

</LinearLayout>

Why is this happening? 为什么会这样呢?

I was checking if it was some sort of PADDING or MARGIN from the LINEAR LAYOUT or from the TEXTVIEW itself, looks like it's not. 我检查,如果它是某种PADDINGMARGINLINEAR LAYOUT还是从TEXTVIEW本身,貌似不是这样的。

But I was able to align it correctly by adding android:layout_gravity="center" on the TEXTVIEW . 但是我能够通过在TEXTVIEW上添加android:layout_gravity="center"来正确对齐它。

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="18sp"
    android:layout_gravity="center"
    android:text="long text over multiple lines ..."/>

Some values other than center works too. center以外的某些值也可以使用。

You can use 您可以使用

android:singleLine="false"

to require textview wrap down if string over with 如果字符串超过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM