android - 如何让我的开关按钮自行居中
问题描述
我不确定是否可以这样做,但请查看此切换按钮: 切换按钮位置
所以我要做的是将它移动到确切的中心,它应该在红色正方形位置:中心位置
只是为了让你知道我已经尝试过:
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>
那么有什么方法可以做我想做的事吗?
解决方案
替换LinearLayout
为RelativeLayout
。
<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>
推荐阅读
- express - axios 通过数据传递获取请求,但未正确传递数据
- java - Android Studio 警告整数乘法隐式转换为 long。如何解决这个问题?
- c# - 将新项目添加到绑定的 ItemsSource 时,WinUI 3 UWP TabView 不显示新选项卡
- laravel - Laravel 8:使用原始文件名和扩展名的文件上传
- python-3.x - 我正在研究 mmdetection 实例分割模型,但是当我尝试实现 HTC 和 SCNet 等模型时,它显示错误
- javascript - 编辑 Chrome 扩展程序
- node.js - Cypress + Gapi.js 身份验证测试
- azure - 从 powershell 触发计时器触发 Azure 函数
- python - 在 PyOpengl 中将文本绑定到对象
- django-rest-framework - 如何从 DRF 的标题选项中删除“位置”?