Android drawable 与 mipmap 文件夹存放图片有区别

简介: Android drawable 与 mipmap 文件夹存放图片有区别

一、正常使用

  • 代码
<!-- CheckBox -->
<CheckBox
    android:id="@+id/man"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="男"/>
<!-- CheckBox -->
<CheckBox
    android:id="@+id/woman"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="女"/>
  • 如果需要 设置文字与选择框的距离,加上 android:paddingLeft="5dp" 配置即可。
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获得按钮
        CheckBox manBox = (CheckBox) findViewById(R.id.man);
        CheckBox womanBox = (CheckBox) findViewById(R.id.woman);
        // 添加选中监听
        manBox.setOnCheckedChangeListener(this);
        womanBox.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        // 获取内容
        String str = compoundButton.getText().toString();
        // 是否选中
        if (compoundButton.isChecked()) {
            Toast.makeText(this, str, Toast.LENGTH_LONG).show();
        }
    }
}
  • 效果

二、drawable 自定义

<!-- CheckBox -->
<CheckBox
    android:id="@+id/none"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="无"
    android:button="@drawable/check_box"/>
  • drawable 代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_checked="false"
        android:drawable="@drawable/box_normal"/>
    <item
        android:state_enabled="true"
        android:state_checked="true"
        android:drawable="@drawable/box_check"/>
</selector>
  • 效果

二、style 自定义

  • 它还是基于 drawable 代码
  • xml 代码
<!-- CheckBox -->
<CheckBox
    android:id="@+id/none"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/MyCheckBox"
    android:text="无"/>
  • drawable 代码,需要在 res/values/styles.xml 文件中添加,如果没有 styles.xml 手动创建一下。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyCheckBox">
        <!-- 导入 drawable 文件中的 check_box -->
        <item name="android:button">@drawable/check_box</item>
    </style>
</resources>
  • 效果与 drawable 一致。
相关文章
|
3月前
|
安全 Android开发 数据安全/隐私保护
请说明鸿蒙操作系统与其他操作系统(如Android和iOS)的主要区别。
请说明鸿蒙操作系统与其他操作系统(如Android和iOS)的主要区别。
59 1
|
3月前
|
Android开发
Android基础知识:请解释Service是什么,它与IntentService的区别是什么?
Android基础知识:请解释Service是什么,它与IntentService的区别是什么?
39 0
|
3月前
|
数据可视化 Android开发 容器
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
94 5
|
25天前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
22 1
|
2月前
|
Java 测试技术 API
安卓APP和iOS APP在测试上的区别是什么?
安卓APP和iOS APP在测试上的区别是什么?
|
3月前
|
Android开发 容器
Android安卓gravity和layout_gravity的区别
Android安卓gravity和layout_gravity的区别
46 2
|
3月前
|
XML Android开发 数据格式
Android安卓 match_parent与match_parent区别
Android安卓 match_parent与match_parent区别
32 0
|
3月前
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
293 54
|
3月前
|
XML 编解码 Android开发
Android各种各样的Drawable-更新中
Android各种各样的Drawable-更新中
58 0