【问题标题】:How to align two buttons and views left to center, center to right with如何将两个按钮和视图从左到中对齐,从中心到右对齐
【发布时间】:2021-09-20 20:09:07
【问题描述】:

如何创建一个布局,其中两个按钮可以从左侧填充一半的布局,第二个按钮在右侧,下面还有一个视图作为边框?

我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".Login">

    <RelativeLayout
        android:gravity="center_horizontal"
        android:layout_marginTop="70dp"
        android:layout_marginBottom="70dp"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/_45sdp"
            android:background="@color/white"
            android:orientation="horizontal">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <Button
                android:id="@+id/btn_chat"
                android:layout_alignParentLeft="true"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold"
                android:text="LOGIN" />

            <Button
                android:id="@+id/btn_cart"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:textColor="@color/lightDark"
                android:textStyle="bold"
                android:text="REGISTER" />
            </RelativeLayout>
            <View
                android:id="@+id/active_border"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
                android:background="@color/colorPrimary"
                android:layout_width="wrap_content"
                android:layout_height="1dp"/>
            <View
                android:id="@+id/inactive_border"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:background="@color/lightDark"
                android:layout_width="wrap_content"
                android:layout_height="1dp"/>
        </RelativeLayout>

    </RelativeLayout>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout layout


    【解决方案1】:

    您可以使用ConstraintLayout 作为父级并在子级中设置app:layout_constraintWidth_percent="0.5" 以确保它占据宽度的一半。还使用约束确保按钮位于边框视图的顶部。

    例如以下代码使用 ConstraintLayout 来实现类似的功能:

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/borderView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.5" />
    
        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@+id/borderView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.5" />
    
        <View
            android:id="@+id/borderView"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="parent" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多