[英]How can I create custom switches in android with text on both side of switch's track an in thumb?
[英]Android Studio - How can I have switches on the left side of the screen, with the right edges of each switch aligned?
我已经尝试调整这些开关几天了,但找不到答案。 我想使开关本身对齐(使右边缘对齐),但整个对象(文本+开关)位于页面的左侧。
这是我所拥有的,我尝试了重力的不同组合以及不同的布局类型等:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="top"
android:gravity="right">
<Switch
android:id="@+id/sw1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="switch1abc"
android:textAlignment="viewStart"/>
<Switch
android:id="@+id/sw2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="switch2"
android:textAlignment="viewStart"/>
<Switch
android:id="@+id/sw3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="switch3a"
android:textAlignment="viewStart" />
<Switch
android:id="@+id/sw4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="switch4abcdef"
android:textAlignment="viewStart" />
</GridLayout>
这是它的样子
另外,我不太确定“ viewStart”是什么,有人可以解释吗?
谢谢!
我敢打赌,您只能通过使用单独的文本视图和切换视图来实现。 尝试这个:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch one text"/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch two text"/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</GridLayout>
问题是开关宽度不同
layout.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="right"
android:orientation="vertical"
android:padding="16dp">
<Switch
android:id="@+id/sw1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="switch1abc"
android:textAlignment="viewStart" />
<Switch
android:id="@+id/sw2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="switch2"
android:textAlignment="viewStart" />
<Switch
android:id="@+id/sw3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="switch3a"
android:textAlignment="viewStart" />
<Switch
android:id="@+id/sw4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="switch4abcdef"
android:textAlignment="viewStart" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.