线性布局自动换行文本视图

4
我正在设计以下布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/messageText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Some really long text asd asd asd asd asd asd asd asd asd asd asd" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12:45 PM" />

</LinearLayout>

它提供了一个类似上面那个的UI... 这正是我需要的。当文本很长时,它会被正确地包装,而不会像水平方向那样将按钮推开屏幕。按钮也不会被拉伸。
但是如果文本很小,它看起来像这样:
我想要的是按钮应该与文本的左端对齐,像这样:
我知道我将布局权重设置为1。
它会消耗掉给按钮所需的空间后剩余的整个区域。
如果我不这样做,并使宽度为wrap content(自适应宽度),对于短文本,它运行得很好。但随着文本的增长,
它会将按钮从屏幕上推开。
这就是问题所在。 希望我足够描述了我的问题。但当然很长。当然很痛苦。 但非常乐意知道任何指针。
我不排斥使用相对布局。
感激任何帮助。
谢谢
3个回答

7
希望这对您有用。请尝试以下操作:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/messageText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Some really long text asd asd asd asd asd asd asd asd asd asd asd![enter image description here][1]" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="12:45 PM" />

我认为您需要将linearlayout的android:layout_width="fill_parent"更改为android:layout_width="wrap_content"


1

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/messageText"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
     android:layout_weight="1.0"
    android:text="Some really long text asd asd asd asd asd asd asd asd asd asd asd![enter image description here][1]" />

<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="12:45 PM" />


1

我在我的设备上测试过,运行良好。请尝试。

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

<TextView
    android:id="@+id/messageText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="sdgdsgdhdhdfhdhgfffffffffffffffffffffffffffffffffffffffff" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="12:45 PM" />

</LinearLayout>

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