zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android 实现两个控件水平居中

Android 实现 控件 两个 水平 居中
2023-09-14 09:04:24 时间

效果图:
在这里插入图片描述

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_marginTop="80sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/center"
            android:textSize="20dp"
            android:text="左"/>

        <View
            android:id="@+id/center"
            android:layout_width="3dp"
            android:layout_height="40dp"
            android:layout_centerInParent="true"
            android:background="#ff0303"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/center"
            android:textSize="20dp"
            android:text="右"/>

    </RelativeLayout>
    
</LinearLayout>

android:layout_toRightOf 在该id控件的右方
android:layout_toLeftOf 在该id控件的左方