![](/img/trans.png)
[英]RelativeLayout, align left/right for two buttons, align center for one
[英]How to align two buttons and views left to center, center to right with
如何創建帶有兩個按鈕的布局,這些按鈕可以從左側填充布局的一半,右側的第二個按鈕還帶有下方的視圖作為邊框?
我的布局
<?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>
您可以使用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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.