2013-04-05 74 views
2

我设计布局如下线性布局包装的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![enter image description here][1]" /> 

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

</LinearLayout> 

它给像上面的UI ...... Long Text Image

这是完美的我所需要的。由于方向是水平的,所以长按钮不会将屏幕按压离开屏幕。按钮也不拉伸。

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

Short text image

我要的是按钮应该这样

short correct text

我知道,因为我把布局权重为1对齐文本的左端。

在给按钮提供需要的空间后,它会消耗剩下的整个区域。

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

它将按钮从屏幕上推开。

那就是问题所在。 希望我让我的问题有足够的描述性。但当然要阅读很长时间。当然痛苦。 但是,真的很高兴知道任何指针。

我不愿意使用相对布局。

任何帮助将不胜感激。

谢谢

回答

5

希望这适用于你。请试试这个:

<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的 layout_width = “FILL_PARENT” 到的android:layout_width = “WRAP_CONTENT”

+0

谢谢。拯救了我的夜晚:) – Javanator 2013-04-05 13:54:31

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

0

我在我的设备上测试过它并且工作正常。试试吧

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