Search code examples
androidtextviewalignmentandroid-linearlayout

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

  • how I understood it the "dp" is a relative value to a specific resolution (so the split-off by android:layout_weigth should not effect this) and

  • if I remove the margins, they are still not in line.

Has anyone an idea? Do I overlook something obvious?


Solution

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