【发布时间】:2017-01-21 12:00:30
【问题描述】:
我想为我想要的一些单选按钮创建一些自定义的可绘制选择器。下面是一个我想要我想要的可绘制选择器的示例。它们是Public 和Followers 选择器,如下所示:
问题是我无法以这种方式设置选择器的样式,而且我似乎无法弄清楚为什么我无法在未选中的单选按钮上获得带有白色背景的灰色轮廓。
这是我的尝试:
privacy_toggle_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
android:layout_margin="16dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:checkedButton="@+id/offer"
android:id="@+id/privacy_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/pick_out_line"
android:orientation="horizontal">
<RadioButton
android:id="@+id/public_toggle"
android:background="@drawable/toggle_widget_background"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="@string/publicString"
android:textAllCaps="true"
android:textSize="18sp"
android:textColor="@color/white" />
<RadioButton
android:id="@+id/followers_toggle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/toggle_widget_background"
android:button="@null"
android:gravity="center"
android:text="@string/followers"
android:checked="true"
android:textSize="18sp"
android:textAllCaps="true"
android:textColor="@color/white" />
</RadioGroup>
drawable/pick_out_line.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="@color/teal" />
<stroke
android:width="5dp"
android:color="@color/material_color_grey_400" />
</shape>
toggle_widget_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/teal" android:state_checked="true" />
<item android:drawable="@color/teal" android:state_pressed="true" />
<item android:drawable="@color/white" />
</selector>
我认为在pick_out_line.xml 中,将stroke 设置为5dp 应该会使边框变灰,但它不会在RadioButton 上显示为灰色。此外,我似乎无法弄清楚如何使未选择的RadioButton 具有白色背景和灰色文本。如果有人可以帮助我,那就太好了。谢谢。
【问题讨论】:
标签: android layout drawable android-drawable custom-selectors