如何在布局的中心准确显示 Switch 小部件?
How to Show Switch widget exactly in the center of the layout?
我正在尝试在我的布局中添加开关小部件。
我遇到的问题是小部件出现在右侧,其文本为 space(但我不想要任何文本。它看起来像这样。
enter image description here
但我想要的是
enter image description here
我尝试了一些方法,比如使用嵌套布局(它适用于小屏幕,但当我在选项卡上尝试时,由于大屏幕,它获得更多 space,最终它略微向右移动离开space 用于文本,即使我没有输入任何文本)。
我目前使用的 xml 代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal"
android:weightSum="11">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"></LinearLayout>
<Switch
android:id="@+id/sosswitch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"></LinearLayout>
</LinearLayout>
我正在使用上面的代码将开关置于中间,但它在大屏幕上仍然会产生问题。
由于仅使用 LinearLayout
没有限制,因此使用 RelativeLayout
将解决您的问题。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
>
<Switch
android:id="@+id/sosswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="" />
</RelativeLayout>
问题是由于在线性布局中使用 weight
系统导致 Switch
元素的宽度扩展。要在此处将 Switch
居中对齐,使用 layout_centerInParent
。
我正在尝试在我的布局中添加开关小部件。 我遇到的问题是小部件出现在右侧,其文本为 space(但我不想要任何文本。它看起来像这样。
enter image description here
但我想要的是
enter image description here
我尝试了一些方法,比如使用嵌套布局(它适用于小屏幕,但当我在选项卡上尝试时,由于大屏幕,它获得更多 space,最终它略微向右移动离开space 用于文本,即使我没有输入任何文本)。
我目前使用的 xml 代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal"
android:weightSum="11">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"></LinearLayout>
<Switch
android:id="@+id/sosswitch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"></LinearLayout>
</LinearLayout>
我正在使用上面的代码将开关置于中间,但它在大屏幕上仍然会产生问题。
由于仅使用 LinearLayout
没有限制,因此使用 RelativeLayout
将解决您的问题。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
>
<Switch
android:id="@+id/sosswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="" />
</RelativeLayout>
问题是由于在线性布局中使用 weight
系统导致 Switch
元素的宽度扩展。要在此处将 Switch
居中对齐,使用 layout_centerInParent
。