3

I use android: layout_weight = "1" to align the elements, but I can't figure out why I need to specify the unit of measurement.

What is the reason for this?

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp">
        <Button
            android:text="+"
            android:layout_width="odp"
            android:layout_height="65dp"
            android:layout_weight="1"// requires a unit
            android:background="@android:color/black"
            android:textColor="@android:color/white"
            android:id="@+id/bttnPlus"
            android:layout_marginRight="20.0dp" />
        <Button
            android:text="-"
            android:layout_width="odp"
            android:layout_height="65dp"
            android:layout_weight="1"// requires a unit
            android:background="@android:color/black"
            android:textColor="@android:color/white"
            android:layout_marginRight="20.0dp"
            android:id="@+id/bttnMinuse" />
    </LinearLayout>
bahmN
  • 47
  • 5
  • I can confirm that it will work well with just 1. And this is a good question as I found it strange too, just I have never asked. – Ivan Ičin Dec 07 '20 at 20:33

2 Answers2

1

It does not need a unit and it's an issue of Visua-Studio.

In this thread:

It should be fixed in the Visual-Studio v16.9 Preview.

nevermore
  • 15,432
  • 1
  • 12
  • 30
0

I think the problem isn't the layout_weight. your layout_width has typos

android:layout_width="0dp"

instead of

android:layout_width="odp"
Sven-Michael Stübe
  • 14,560
  • 4
  • 52
  • 103