将TextView强制显示多行文本

3

这是对此问题的追问:

强制TextView换行而不使用\n

我有一个声明如下的TextView:

<TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:singleLine="false"/>

在代码中,我设置了一个很长的文本,但是文本视图只显示成一行而不是分成多行。在链接的问题中接受的答案建议设置android:maxWidth属性,但我不想那样做。我想让文本行自动换行,如果文本长度超过了在文本视图声明中设置的0.4权重。有没有一种方法可以这样做而不使用固定大小?
按要求,这是带有其父级的文本视图:
<LinearLayout
    android:id="@+id/linearLayoutBottomData"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayoutTopData"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="@dimen/large_font_size"
        android:singleLine="false"/>

    <TextView
        android:id="@+id/myTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="@dimen/large_font_size"/>
</LinearLayout>

@CristianAndreiGrigore 那个没起作用。 - CodeMonkey
@ADM layout_weight 是用于宽度而非高度(文本视图位于水平线性布局内)。我希望文本视图在宽度上保持0.4的权重,并使文本仅在此大小范围内换行。 - CodeMonkey
你能发布TextView的父布局吗?包括所有子视图。 - Nongthonbam Tonthoi
@NongthonbamTonthoi 添加了 - CodeMonkey
将“TextView”的宽度设置为“0dp”,如果您的父布局方向是水平的,它就会起作用。 - ADM
显示剩余3条评论
1个回答

5
请使用我在注释中提到的以下布局。
<LinearLayout
android:id="@+id/linearLayoutBottomData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayoutTopData"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="1">

<TextView
    android:id="@+id/myTextView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:gravity="left|start"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="@dimen/large_font_size"
    android:singleLine="false"/>

<TextView
    android:id="@+id/myTextView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:gravity="left|start"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="@dimen/large_font_size"/>


很好。我给你点赞。 - CodeMonkey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接