简体   繁体   中英

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

简短的正确文本

I know as i put the layout weight to 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"

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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