简体   繁体   English

线性布局包装 TextView

[英]Linear Layout Wrapping TextView

I am designing a layout as following我正在设计一个布局如下

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

It gives a UI like the above...它提供了一个像上面这样的用户界面......

长文本图像

Which is perfect what i need.这是我需要的完美。 Long text is getting wrapped properly without pushing Button away from the screen as orientation is horizontal.由于方向是水平的,因此无需将 Button 推离屏幕即可正确包裹长文本。 button is also not stretching.按钮也没有拉伸。

But if text is small.但是如果文字很小。 It looks like this看起来像这样

短文本图片

What i want is Button should be aligned to the left end of the text like this我想要的是 Button 应该像这样与文本的左端对齐

简短的正确文本

I know as i put the layout weight to 1.我知道我将布局权重设为 1。

It consumes the whole area left after giving needed space to the button.在为按钮提供所需空间后,它会占用整个区域。

If i don't put that.如果我不放那个。 and make the width to be wrap content.并使宽度成为包装内容。 It works great for short text.它适用于短文本。 but as text grows.但随着文本的增长。

it pushes the button away from the screen.它将按钮推离屏幕。

Thats the problem.那就是问题所在。 Hope i made my problem enough descriptive.希望我使我的问题具有足够的描述性。 But of course long to read.但当然要读很久。 Painful of course.当然痛苦。 But will be really glad to know any pointer.但是会很高兴知道任何指针。

I am not reluctant to use relative layout as well.我也不愿意使用相对布局。

Any help will be appreciated.任何帮助将不胜感激。

Thanks谢谢

Hope this works for you.希望这对你有用。 please try this:请试试这个:

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

I think you need to change android:layout_width="fill_parent" of linearlayout to android:layout_width="wrap_content"我认为您需要将线性布局的 android:layout_width="fill_parent"更改为android:layout_width="wrap_content"

Try this:试试这个:

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

i tested it on my device and working fine.我在我的设备上测试了它并且工作正常。 try it尝试一下

<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