How to align elements horizontally inside LinearLayout?

enter image description here

I can not control the height LinearLayout. They will not be aligned correctly and will not fill the width. I want the divider to be in the center and both buttons on both sides. Here is my code:

<LinearLayout
    android:id="@+id/buttonFieldsLayout"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loginFieldsLayout" >

    <Button
        android:id="@+id/signUpButton"
        style="@style/AuthButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sign_up_button_label" />

    <View
        android:id="@+id/buttonDivider"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="@drawable/divider" />

    <Button
        android:id="@+id/cancelButton"
        style="@style/AuthButton"
        android:text="@string/cancel_button_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient 
    android:startColor="@color/white"
    android:centerColor="@color/blue"
    android:endColor="@color/white" />
</shape>

Update 1:

View still sticks out after @Onik suggestion

enter image description here

Update 2:

I deleted the item Viewand added this code to LinearLayoutand it will work!

android:divider="@drawable/divider"
android:showDividers="middle"

Note. android:dividerthe property is only available in Android3.0 (API level 11) or higher.

Actually this link helped me and showed me the correct way to put a separator: How to add a (vertical) divider to a horizontal LinearLayout?

+4
2

layout_weight Buttons. divider, (, ), layout_height="match_parent" layout_height="wrap_content". ( ) button's , divider :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonFieldsLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loginFieldsLayout" >

<Button
    android:id="@+id/signUpButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="sign_up_button_label" />

<View
    android:id="@+id/buttonDivider"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@drawable/divider" />

<Button
    android:id="@+id/cancelButton"
    android:text="cancel_button_label"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

+2

RelativeLayout android:layout_width="match_parent".

android:layout_centerHorizontal="true"

:

android:layout_toRightOf="@id/buttonDivider"

... ...

android:layout_toLeftOf="@id/buttonDivider"
+1

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


All Articles