自定义Android复选框

时间:2015-11-18 08:58:17

标签: android checkbox

我需要自定义我的复选框。我需要的是用白色填充复选框,并检查必须是黑色。我已经按照几个例子,没有按预期工作。

这是我的.xml

 <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:button="@drawable/check_box_style"/>

你能告诉我我的check_box_style应如何设计。感谢。

或者如果有人能解释我如何做到这一点我很有意思。

- 编辑 -

11-18 15:49:56.970  16291-16291/com.myayubo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myayubo, PID: 16291
android.view.InflateException: Binary XML file line #28: Error inflating class android.widget.CheckBox
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at com.myayubo.CustomAdapter.getView(CustomAdapter.java:25)
        at android.widget.AbsListView.obtainView(AbsListView.java:2338)
        at android.widget.ListView.makeAndAddView(ListView.java:1812)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillFromTop(ListView.java:759)
        at android.widget.ListView.layoutChildren(ListView.java:1645)
        at android.widget.AbsListView.onLayout(AbsListView.java:2149)
        at android.view.View.layout(View.java:15148)
        at android.view.ViewGroup.layout(ViewGroup.java:4866)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15148)
        at android.view.ViewGroup.layout(ViewGroup.java:4866)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)
        at android.view.View.layout(View.java:15148)
        at android.view.ViewGroup.layout(ViewGroup.java:4866)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15148)
        at android.view.ViewGroup.layout(ViewGroup.java:4866)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
        at android.view.View.layout(View.java:15148)
        at android.view.ViewGroup.layout(ViewGroup.java:4866)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15148)
        at android.view.ViewGroup.layout(ViewGroup.java:4866)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15148)
        at android.view.ViewGroup.layout(ViewGroup.java:4866)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15148)
        at android.view.ViewGroup.layout(ViewGroup.java:4866)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2336)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2042)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1208)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6274)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:560)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
        at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您需要两个具有所需复选框外观的绘图并定义可绘制选择器,如下所述: https://stackoverflow.com/a/7783892/1099716

有很多VectorDrawable图标生成器以及png生成器可以用作drawables。其中一个可以在这里找到:https://materialdesignicons.com/

还有针对android 14+ https://github.com/wnafee/vector-compat

的VectorDrawable compat

有关drawables使用的更多信息,请查看以下教程http://www.vogella.com/tutorials/AndroidDrawables/article.html

答案 1 :(得分:1)

经过一些研究后我可以看到,这是你最好的选择

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:buttonTint="@android:color/black"/>

它适用于我,但我不确定它是否适用于你,希望这有帮助!

答案 2 :(得分:0)

您可以将状态设置为

的android drawable xml
 <?xml version="1.0" encoding="utf-8"?>
 <selector  xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="false" android:drawable="@drawable/yourdrawable1" />
 <item android:state_checked="true" android:drawable="@drawable/yourdrawable2" />
 <item android:drawable="@drawable/yourdrawable1" /> <!-- default state -->

然后你需要指定它:android:button或android:background