开发者

Android: Layout problem with 2 textviews containing long text

I have problems getting two Textviews to behave the way I want.

I want one on the left side, the other one on the right side. I fairly simply achieved this with a horizontal linear layout. Both of them should only use half of the horizontal space ("stay on their side") and expand vertically if the string is too long.

However, if one or both of the text views contain a very long string, I get weird results depending on how I set weight.

E.g. if I set weight to Left=1, Right=1 everything works fine if both strings are short or both strings are long. If they differ, however, the short one is shrinked to only show on character each line. Using 0/0 weight, the shorter TextView just completely vanishes.

I can get it to work if I set the short string text view to "0" and the long one to "1" or higher, but that only covers one case.

I also experimented with TableLayout and RelativeLayout but nothing worke开发者_如何学Cd.

Kind regards, jellyfish

Edit: Solved it by putting the TextViews into vertical LinearLayouts inside the horizontal Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:orientation="horizontal">

    <LinearLayout
        android:layout_height="wrap_content" 
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_gravity="right">
        </TextView>

    </LinearLayout>

    <LinearLayout
        android:layout_height="wrap_content" 
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_gravity="right">
        </TextView>

    </LinearLayout>

</LinearLayout>

Seems a bit oversized to me, though... but apparently there's no other solution?


This works fine with me if I set one letter string to the first textView and very long string to the second textView

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
    <TextView android:id="@+id/textView1" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="1"></TextView>
    <TextView android:id="@+id/textView2" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="1"></TextView>
</LinearLayout>


In order for the android:layout_weight to work, you need to set the dimension you want weighted to 0. In addition, although it is not required, it helps to have your total weights add up to a value that you can easily decipher as a percentage. I like to use fractions and have the total be 1. Others like to use 100 as the total. So for two side by side edit texts that take half the layout each:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:orientation="horizontal">

        <TextView  
            android:layout_width="0dip" 
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:gravity="right"
            android:layout_gravity="right">
        </TextView>

        <TextView  
            android:layout_width="0dip" 
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:gravity="right"
            android:layout_gravity="right">
        </TextView>
</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜