一、基础属性
RadioButton
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 | 默认选中该选项 |
1、layout_width
2、layout_height
组件宽度和高度有4个可选值,如下图:
3、id
// activity_main.xml
android:id="@+id/btn1" // 给当前控件取个id叫btn1
// MainActivity.java
Button btn1=findViewById(R.id.btn1); // 按id获取控件
btn1.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的条件下才有效。
程序示例:
<RadioButton
android:id="@+id/rb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="男"
android:textColor="@color/blue_700"
android:textSize="50sp"
android:background="@color/blue_50">
</RadioButton>
<RadioButton
android:id="@+id/rb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="女"
android:textColor="@color/i_purple_500"
android:textSize="50sp"
android:background="@color/i_purple_200">
</RadioButton>
<RadioButton
android:id="@+id/rb3"
android:layout_width="match_parent"
android:layout_height="wrap_content