Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear Layout Does not align baselines and Relative layout doesn't allow weights

Tags:

android

I have a Linear layout with vertical orientation in which I'm inserting several view groups. One of them is:

<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 a view that looks like this (apologies for the link, I wish there was a better way). This is unacceptable as the Button does not align with the EditText.

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

I guess what I'm trying to achieve is: (1) The baseline of the Button is aligned with the other controls in the row. (2) The two button are of the same width.

I seem to be able to achieve (1) with RelativeLayout and (2) with LinearLayout. But not both.

Can anyone help me please?

like image 332
Code Poet Avatar asked Feb 23 '23 07:02

Code Poet


2 Answers

use

android:gravity="center_vertical"

in your linearlayout. All items will be centered

like image 167
njzk2 Avatar answered Feb 24 '23 20:02

njzk2


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

like image 40
user994886 Avatar answered Feb 24 '23 21:02

user994886