Line layout Does not align source lines, and relative layout does not allow weights

I have a linear layout with a vertical orientation into which I insert several groups of views. One of them:

<LinearLayout android:id="@+id/addedit_updowncontrol" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_marginBottom="5dp" android:orientation="horizontal"> <Button android:id="@+id/addedit_btndecrement" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_weight="0.1" android:text="-" /> <EditText android:id="@+id/addedit_txtQuantity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="90dp" android:layout_weight="0.4" android:layout_alignParentTop="true" android:inputType="numberDecimal" /> <Button android:id="@+id/addedit_btnIncrement" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_weight="0.1" android:layout_toLeftOf="@+id/addedit_units" android:text="+" /> <Spinner android:id="@+id/addedit_units" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.4" android:layout_alignParentTop="true" android:layout_alignParentRight="true" /> </LinearLayout> 

This gives me an idea that looks like this (sorry for the link, I would like a better way). This is unacceptable because the button does not align with EditText.

I tried switching to RelativeLayout, which gives me something like this . This is unacceptable because the two buttons (-) and (+) do not have the same width.

I assume that I am trying to achieve: (1) The baseline of the button is aligned with the other controls in the line. (2) Two buttons have the same width.

It seems I can achieve (1) with RelativeLayout and (2) with LinearLayout . But not both.

Can anybody help me?

+4
source share
5 answers

use

 android:gravity="center_vertical" 

in your linear mode. All elements will be centered.

+4
source

You can try a few things. First set the horizontal orientation of LinearLayout. Have you tried setting RelativeLayout inside LinearLayout? Perhaps this also works: set the buttons to wrap_content and edit_text to fill_parent?

0
source

You have specified many useless attributes for LinearLayout . For example, you do not need all of these layout_alignParentTop , layout_toLeftOf , etc. for LinearLayout , they are used in RelativeLayout . Try the following:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/addedit_updowncontrol" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_below="@+id/textView2" android:orientation="horizontal" > <Button android:id="@+id/addedit_btndecrement" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" android:text="-" /> <EditText android:id="@+id/addedit_txtQuantity" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" android:minWidth="90dp" android:inputType="numberDecimal" /> <Button android:id="@+id/addedit_btnIncrement" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" android:text="+" /> <Spinner android:id="@+id/addedit_units" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" /> </LinearLayout> 
0
source

Change the orientation of the linear layout to horizontal and set the baselinealigned attribute to true in xml. Play with layout_weights. Assign the same weight to both buttons. Hope this helps

0
source

I know this is not very convenient, but keep LinearLayout and add paddingTop = "2dp" or marginTop = "2dp" to EditText and Spinner.

The 9patch attributes for these elements include padding at the bottom, which makes them look like they don't match everything else.

(I think its 2dp, it could be 3dp)

Not sure if this is still a problem for EditText in version 3.0+, as the stock design for EditText is very different in the bare topic

0
source

Source: https://habr.com/ru/post/1382913/


All Articles