Android开发基础之控件CheckBox

       

一、基础属性

1、layout_width 宽度
2、layout_height 高度
3、id 设置组件id
4、text 设置显示的内容
5、textColor 设置字体颜色
6、textStyle 设置字体风格:normal(无效果)、bold(加粗)、italic(斜体)
7、textSize 字体大小,单位常用sp
8、background 控件背景颜色
9、checked 默认选中该选项
10、orientation 内部控件排列的方向,例如水平排列或垂直排列
11、paddingXXX 内边距,该控件内部控件间的距离
12、background 控件背景颜色或图片

       
1、layout_width
2、layout_height

        组件宽度和高度有4个可选值,如下图:
在这里插入图片描述

       
3、id

// activity_main.xml
android:id="@+id/cb1"  // 给当前控件取个id叫cb1
// MainActivity.java
CheckBox cb1=findViewById(R.id.cb1);  // 按id获取控件
cb1.setText("hh");                       // 对这个控件设置显示内容

        如果在.java和.xml文件中对同一属性进行了不同设置,比如.java中设置控件内容hh,.xml中设置内容为aa,最后显示的是.java中的内容hh。

       
4、text
       可以直接在activity_main.xml中写android:text="嘻嘻",也可以在strings.xml中定义好字符串,再在activity_main.xml中使用这个字符串。

// strings.xml
<string name="str1">嘻嘻</string>
// activity_main.xml
android:text="@string/str1"

       
5、textColor
       与text类似,可以直接在activity_main.xml中写android:textColor="#FF0000FF",也可以在colors.xml中定义好颜色,再在activity_main.xml中使用这个颜色。

       
       

9、checked
       checked=“true”,默认这个RadioButton是选中的。该属性只有在RadioGroup中每个RadioButton都设置了id的条件下才有效。

       

       

10、orientation

内部控件的排列方式:

  • orientation=“vertical”,垂直排列
  • orientation=“horizontal”,水平排列

       

11、paddingXXX

内边距,该控件与内部的控件间的距离,常用的padding有以下几种:

  • padding,该控件与内部的控件间的距离
  • paddingTop,该控件与内部的控件间的上方的距离
  • paddingBottom,该控件与内部的控件间的下方的距离
  • paddingRight,该控件与内部的控件间的左侧的距离
  • paddingLeft,该控件与内部的控件间的右侧的距离

       

程序示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:text="你喜欢做什么?"
        android:textSize="50sp"
        android:textColor="@color/i_purple_700">
    </TextView>
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cb1"
        android:text="吃饭"
        android:textSize="50sp"
        android:checked="true">
    </CheckBox>
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cb2"
        android:text="睡觉"
        android:textSize="50sp"
        android:checked="true">
    </CheckBox>
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cb3"
        android:text="工作"
        android:textSize="</
  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值