繁体   English   中英

线性布局包装 TextView

[英]Linear Layout Wrapping TextView

我正在设计一个布局如下

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

它提供了一个像上面这样的用户界面......

长文本图像

这是我需要的完美。 由于方向是水平的,因此无需将 Button 推离屏幕即可正确包裹长文本。 按钮也没有拉伸。

但是如果文字很小。 看起来像这样

短文本图片

我想要的是 Button 应该像这样与文本的左端对齐

简短的正确文本

我知道我将布局权重设为 1。

在为按钮提供所需空间后,它会占用整个区域。

如果我不放那个。 并使宽度成为包装内容。 它适用于短文本。 但随着文本的增长。

它将按钮推离屏幕。

那就是问题所在。 希望我使我的问题具有足够的描述性。 但当然要读很久。 当然痛苦。 但是会很高兴知道任何指针。

我也不愿意使用相对布局。

任何帮助将不胜感激。

谢谢

希望这对你有用。 请试试这个:

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

我认为您需要将线性布局的 android:layout_width="fill_parent"更改为android:layout_width="wrap_content"

试试这个:

<?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" />

我在我的设备上测试了它并且工作正常。 尝试一下

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

暂无
暂无

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

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