Valentino Ru
Valentino Ru

Reputation: 5052

android two linearlayouts with equivalent partition via android:layout_weight not in line

I want to display three different values allocated to two different headers, in other words a first header with one value and a second one with two values

My approach is to split the "header linear layout" 40/60 and the "values linear layout" 40/30/30. In order to not display the text beginning at the border, I put everywhere an android:layout_marginLeft="15dp" in, so finally it should remain proportional.

In my XML, I declare the following:

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView 
        android:layout_weight="4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="@string/txt_price_chf"
        />
    <TextView 
        android:layout_weight="6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="@string/txt_price_offshore_chf"
        />
</LinearLayout>

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView android:id="@+id/tv_price_ch"
        android:layout_weight="4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="Value1"
        />

    <TextView android:id="@+id/tv_price_off_chf"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="Value2"
        />

    <TextView android:id="@+id/tv_price_off_eur"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="Value3"
        />
</LinearLayout>

The result is following output:

enter image description here

Why are the "Price offshore:"-TextView and the "Value2"-TextView not in line? Note that "Price offshore" is saved in my strings.xml, there is no error like a whitespace or something. First I thought, it's caused by the android:layout_marginLeft="15dp", but

Has anyone an idea? Do I overlook something obvious?

Upvotes: 0

Views: 1078

Answers (3)

Anu
Anu

Reputation: 562

Or another alternative is this:

        <LinearLayout
       android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_weight=".60"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="txt_price_chf" />

    <TextView
        android:id="@+id/tv_price_ch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Value1" />
    </LinearLayout>


      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginLeft="10dp"
          android:layout_weight=".40"
          android:orientation="vertical" >

        <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="txt_price_offshore_chf" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_price_off_chf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Value2" />

        <TextView
            android:id="@+id/tv_price_off_eur"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Value3" />
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Shashank Degloorkar
Shashank Degloorkar

Reputation: 3231

<TextView 
        android:layout_weight="4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="-10dp"
        android:text="@string/txt_price_chf"
        />

This will work for you...

Upvotes: 1

Mattias Isegran Bergander
Mattias Isegran Bergander

Reputation: 11909

Not really something you should be doing with a LinearLayout though.

I suggest using a RelativeLayout, there you can just say that those two should align.

Without digging into details in this particular case, it can be either marginRight, padding etc or just simply a side effect of the non monospaced font.

Upvotes: 0

Related Questions