繁体   English   中英

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

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

我发现以下问题。 像这样简单的东西可以工作:

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

但是,如果您在布局中还有另一个组件,则TextView会开始表现异常。 文本超出容器框架似乎很短:

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

为什么会这样呢?

我检查,如果它是某种PADDINGMARGINLINEAR LAYOUT还是从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 ..."/>

center以外的某些值也可以使用。

您可以使用

android:singleLine="false"

如果字符串超过

暂无
暂无

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

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