如何在 LinearLayout 中对齐视图?

How can I align views justify in LinearLayout?

我想左右对齐两个按钮,我对两个按钮都使用 layout_weight 属性 宽度 .25。但是当我这样做时,按钮宽度变成了 %50.

这就是我想要的:

这是我拥有的:

这是我的 XML 布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <ImageButton
        android:id="@+id/like"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".25"
        android:textColor="#212121"
        android:background="@drawable/button_default"
        android:layout_margin="5dp"
        android:src="@drawable/like" />

    <Button
        android:text="NEXT"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:textColor="#FFFFFF"
        android:background="@drawable/button_primary"
        android:layout_margin="5dp"/>
</LinearLayout>

我该怎么做?

试试这个代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:weightSum="2">

        <ImageButton
            android:id="@+id/like"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@mipmap/ic_launcher"
            android:src="@mipmap/ic_launcher"
            android:textColor="#212121" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@mipmap/ic_launcher"
            android:text="NEXT"
            android:textColor="#FFFFFF" />
    </LinearLayout>

</RelativeLayout>

这样做:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/like"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:background="@drawable/button_default"
        android:src="@drawable/like"
        android:textColor="#212121" />

    <Space
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/button_primary"
        android:text="NEXT"
        android:textColor="#FFFFFF" />
</LinearLayout>

或者像这样:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageButton
        android:id="@+id/like"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="5dp"
        android:background="@drawable/button_default"
        android:src="@drawable/like"
        android:textColor="#212121" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="5dp"
        android:background="@drawable/button_primary"
        android:text="NEXT"
        android:textColor="#FFFFFF" />
</RelativeLayout>