首页 > 解决方案 > 如何让我的开关按钮自行居中

问题描述

我不确定是否可以这样做,但请查看此切换按钮: 切换按钮位置

所以我要做的是将它移动到确切的中心,它应该在红色正方形位置:中心位置

只是为了让你知道我已经尝试过:

android:gravity="center" // didn't work
android:layout_gravity="center"// didn't work
android:foregroundGravity="center"// didn't work

这是我的完整 xml 文件:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="25">

        </android.support.constraint.ConstraintLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.37">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="30"
                app:srcCompat="@drawable/ubersign" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="10"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="40"
                    android:text="Driver"
                    android:textAlignment="center" />

                <Switch
                    android:id="@+id/switch3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="20"
                    android:onClick="switchOnOf" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="40"
                    android:text="Rider"
                    android:textAlignment="center" />
            </LinearLayout>

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="0"
                android:text="Get Started" />

        </LinearLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="25">

        </android.support.constraint.ConstraintLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

那么有什么方法可以做我想做的事吗?

标签: androidandroid-layout

解决方案


替换LinearLayoutRelativeLayout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_toLeftOf="@+id/switch3"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="Driver"
        android:textAlignment="center" />

    <Switch
        android:id="@+id/switch3"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
        android:onClick="switchOnOf" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/switch3"
        android:text="Rider"
        android:textAlignment="center" />
</RelativeLayout>

推荐阅读