I want to have two widgets: a spinner and checkBox were placed in one line. I read a lot here and enSO questions and answers and did this markup:

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal"> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_weight="0.1" android:entries="@array/filter_list" /> <CheckBox android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" /> </LinearLayout> 

I had to change the android:layout_weight parameter from spinner from 1 to 0.1 because it turned out that it was on the floor of the screen, and I needed it to occupy about 85% of the entire selected width of the layout. But I'm not sure what I did right, and decided to ask here an opinion on my decision. I hope that I did not have a lot of mistakes.

  • For such tasks, ConstraintLayout is best suited, in which to that, not so long ago, it was possible to specify the parameters immediately in percent - pavlofff

1 answer 1

Wrong. It turns out about 9%, not 85%. In general, it is better to use integers (I'm not sure that android:layout_weight can be used in android:layout_weight ).

  • It is considered so: we add all android:layout_weight which are available. This is 100%.
  • We distribute to each element according to its weight.

So for 85% with two elements, 85% of the total should be allocated. For example, give spinner android:layout_weight="85" , and checkbox the remaining android:layout_weight="15" . Applying arithmetic we get for spiner 17, and for checkbox 3. It will turn out exactly 85% (3 = 85% of the sum 3 and 17).

  <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal"> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_weight="17" android:entries="@array/filter_list" /> <CheckBox android:layout_width="match_parent" android:layout_weight="3" android:layout_height="match_parent" /> </LinearLayout> 

Ps. Smoked manual . The parameter android:layout_weight can also use fractional numbers.

May be a floating point value, such as "1.2".

  • Changed the parameters, and then accidentally turned out the opposite. - Enikeyschik
  • But if we change the weight values ​​of the spinner and the checkbox, it shows everything as it should - Andrew Goroshko
  • Yes, I got it mixed up :) Changed already. - Enikeyschik
  • I didn’t need to be changed)) I have now since you had before the changes)) that is, the check is pressed against the right edge by the spinner - Andrew Goroshko
  • Well, maybe. I am confused as to whom it is necessary :) But I hope the technique is clear. - Enikeyschik