2015-05-14 27 views
2

我试图模仿手机上设置应用中的CheckBox。 它看起来像这样: CheckBox in my Galaxy S4 5.0.1 Settings appAndroid中CheckBox右侧的文本

我使用单独的TextView试过了,但这种方式只勾选可点击,而不是文本和复选标记。
有没有办法做到这一点?
我也试过CheckedTextView,但找不到合适的drawable。

回答

2

尝试下面的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="10" 
    tools:context="com.example.pager.MainActivity" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:text="Anything you want here" /> 

    <CheckBox 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="8" /> 

</LinearLayout> 

它工作正常

0

据我所知,这应该工作

<?xml version="1.0" encoding="utf-8"?> 
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android" 
    android:text="Text" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:button="@null" 
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/> 

随着android:button="@null"删除标准复选框按钮/图像,和之后只需添加复选框图像作为右侧绘图(或使用drawableEnd来支持RTL)

android:drawableRight="?android:attr/listChoiceIndicatorMultiple"

3

有没有办法做到这一点? 我也试过CheckedTextView,但找不到合适的drawable来使用。

CheckedTextView将完美地解决您的问题:

请认准例如在simple_list_item_checked.xml(这是Android的嵌入式布局)

CheckedTextView有下一个属性:

<!-- Drawable used for the check mark graphic. --> 
<attr name="checkMark" format="reference"/> 

所以你只需要将checkMark设置为合适的选择器(如果您没有资源,则可以使用btn_check.xml。它位于android内部):

<CheckedTextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:checkMark="@drawable/your_selector_with_proper_checked_states" 
/>