0


Android 实现自定义样式CheckBox复选框

checkbox这个控件大家都不陌生,直接在xml文件里加个控件就可以了

  1. <CheckBox
  2. android:id="@+id/check_image"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"/>

无非就是改变复选框颜色大小,但原生UI效果不一定能满足我们设计师要求,需要自定义样式

步骤如下:

首先需要用android:button="@null"把原生复选框隐藏起来

然后准备两张 点击前后效果图

在res的drawable下建立一个shape文件 用来定义选中跟取消不同状态效果图

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/ckboxchecked"
  4. android:state_checked="true" />
  5. <item android:drawable="@drawable/ckboxdis" />
  1. </selector>

我们还可以定义渐变色

  1. <!-- <item android:state_checked="true">-->
  2. <!-- <shape>-->
  3. <!-- <gradient android:startColor="#FFFFFF" android:endColor="#000000"/>-->
  4. <!-- </shape>-->
  5. <!-- </item>-->
  6. <!-- <item>-->
  7. <!-- <shape>-->
  8. <!-- <gradient android:startColor="#000000" android:endColor="#FFFFFF" android:angle="-90"/>-->
  9. <!-- </shape>-->
  10. <!-- </item>-->

最后在定义控件时引用这个文件

  1. <CheckBox
  2. android:id="@+id/check_image"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:button="@null"
  6. android:background="@drawable/checkbox_shape" />

我这里复选框后边有个文本框 点击时候整个条目要变色 完整代码如下

  1. <LinearLayout
  2. android:id="@+id/item"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal">
  6. <CheckBox
  7. android:id="@+id/check_image"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:button="@null"
  11. android:background="@drawable/checkbox_shape" />
  12. <TextView
  13. android:id="@+id/text"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center"
  17. android:text="发动机数据"></TextView>
  18. </LinearLayout>

我这里是在fragment使用的对应Java代码如下

  1. private LinearLayout linearLayout;
  2. private CheckBox checkBox;
  3. private int checkout= 0;
  4. @Override
  5. @Nullable
  6. public View onCreateView(LayoutInflater inflater,
  7. @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  8. // TODO Auto-generated method stub
  9. View view = inflater.inflate(R.layout.one_fragment, container, false);
  10. linearLayout = view.findViewById(R.id.item);
  11. checkBox = view.findViewById(R.id.check_image);
  12. checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  13. @Override
  14. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  15. if (isChecked) {
  16. linearLayout.setBackgroundResource(R.color.Copper);
  17. checkout = 1;
  18. } else {
  19. linearLayout.setBackgroundResource(R.color.white);
  20. checkout = 0;
  21. }
  22. }
  23. });
  24. return view;
  25. }

效果图如下

标签: android ui

本文转载自: https://blog.csdn.net/weixin_54723630/article/details/126240914
版权归原作者 常永超 所有, 如有侵权,请联系我们删除。

“Android 实现自定义样式CheckBox复选框”的评论:

还没有评论