如果您想了解android自定义radiobutton文字颜色随选中状态而改变和radiobutton设置选中颜色的知识,那么本篇文章将是您的不二之选。我们将深入剖析android自定义radiobu
如果您想了解android 自定义 radiobutton 文字颜色随选中状态而改变和radiobutton设置选中颜色的知识,那么本篇文章将是您的不二之选。我们将深入剖析android 自定义 radiobutton 文字颜色随选中状态而改变的各个方面,并为您解答radiobutton设置选中颜色的疑在这篇文章中,我们将为您介绍android 自定义 radiobutton 文字颜色随选中状态而改变的相关知识,同时也会详细的解释radiobutton设置选中颜色的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- android 自定义 radiobutton 文字颜色随选中状态而改变(radiobutton设置选中颜色)
- 018 Android 单选按钮 (RadioButton) 和复选框 (CheckBox) 的使用
- Android RadioButton
- Android radiobutton 图片与文字间距的问题
- Android RadioButton 图片位置与大小实例详解
android 自定义 radiobutton 文字颜色随选中状态而改变(radiobutton设置选中颜色)
先看效果
主要是写一个 color selector
在 res / 建一个文件夹取名 color
res/color/color_radiobutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/color_text_selected"/>
<!-- not selected -->
<item android:color="@color/color_text_normal"/>
</selector>
程序中就可以直接使用了
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radiogroup_personal_condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radiobutton_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_radio"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="目录"
android:textColor="@color/color_radiobutton"
android:textSize="@dimen/font_size"
android:text/>
<RadioButton
android:id="@+id/radiobutton_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_radio"
android:button="@null"
android:gravity="center"
android:text="书签"
android:textColor="@color/color_radiobutton"
android:textSize="@dimen/font_size"
android:text/>
018 Android 单选按钮 (RadioButton) 和复选框 (CheckBox) 的使用
1.RadioButton
(1) 介绍
(2) 单选按钮点击事件的用法
(3) RadioButton 与 RadioGroup 配合使用实现单选题功能
(4) xml 布局及使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您最喜欢的城市是" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="北京" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上海" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="广州" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="杭州" />
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
</LinearLayout>
2. 复选框 (CheckBox)
(1) 介绍
(2) xml 文件
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你最喜欢的运动是"/>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="乒乓球" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="羽毛球" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="排球" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="足球" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
(3) java 后台
对应工程名:test23
package com.lucky.test23;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button1;
Button button2;
RadioGroup radioGroup;
CheckBox checkBox1;
CheckBox checkBox2;
CheckBox checkBox3;
CheckBox checkBox4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=findViewById(R.id.button);
button2=findViewById(R.id.button2);
radioGroup=findViewById(R.id.radiogroup);
checkBox1=findViewById(R.id.checkBox);
checkBox2=findViewById(R.id.checkBox2);
checkBox3=findViewById(R.id.checkBox3);
checkBox4=findViewById(R.id.checkBox4);
//绑定按钮点击事件
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i <radioGroup.getChildCount(); i++) { //radioGroup.getChildCount()获取子容器数量
RadioButton radioButton= (RadioButton) radioGroup.getChildAt(i);
//判断按钮是否被选中
if(radioButton.isChecked()){
String str=radioButton.getText().toString();
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
break;
}
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将变量放入数组中便于取用
CheckBox[] cbox={checkBox1,checkBox2,checkBox3,checkBox4};
String str="";
//遍历数组,判断各个复选框的选中情况
for (int i = 0; i <cbox.length ; i++) {
if(cbox[i].isChecked()){
str=str+cbox[i].getText().toString();
}
}
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
});
}
}
3. 效果图
Android RadioButton
1.RadioGroup继承于LinearLayout
<RadioGroup >
<RadioButton/>
...
</RadioGroup>
((RadioButton) radioGroup.getChildAt(index)).setChecked(true);
2.监听器
RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
RadioButton rb = radioGroup.findViewById(id);
int checkedIndex = radioGroup.indexOfChild(rb);
}
}
3.自定义自动换行RadioGroup
import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.RadioGroup; public class MyRadioGroup extends RadioGroup { private static final String TAG = "RadioGroupEx"; public MyRadioGroup(Context context) { super(context); } public MyRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); //调用ViewGroup的方法,测量子view measureChildren(widthMeasureSpec, heightMeasureSpec); //最大的宽 int maxWidth = 0; //累计的高 int totalHeight = 0; //当前这一行的累计行宽 int lineWidth = 0; //当前这行的最大行高 int maxLineHeight = 0; //用于记录换行前的行宽和行高 int oldHeight; int oldWidth; int count = getChildCount(); //假设 widthMode和heightMode都是AT_MOST for (int i = 0; i < count; i++) { View child = getChildAt(i); MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams(); //得到这一行的最高 oldHeight = maxLineHeight; //当前最大宽度 oldWidth = maxWidth; int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin; if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加 //和目前最大的宽度比较,得到最宽。不能加上当前的child的宽,所以用的是oldWidth maxWidth = Math.max(lineWidth, oldWidth); //重置宽度 lineWidth = deltaX; //累加高度 totalHeight += oldHeight; //重置行高,当前这个View,属于下一行,因此当前最大行高为这个child的高度加上margin maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin; Log.v(TAG, "maxHeight:" + totalHeight + "---" + "maxWidth:" + maxWidth); } else { //不换行,累加宽度 lineWidth += deltaX; //不换行,计算行最高 int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin; maxLineHeight = Math.max(maxLineHeight, deltaY); } if (i == count - 1) { //前面没有加上下一行的搞,如果是最后一行,还要再叠加上最后一行的最高的值 totalHeight += maxLineHeight; //计算最后一行和前面的最宽的一行比较 maxWidth = Math.max(lineWidth, oldWidth); } } //加上当前容器的padding值 maxWidth += getPaddingLeft() + getPaddingRight(); totalHeight += getPaddingTop() + getPaddingBottom(); setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth, heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int count = getChildCount(); //pre为前面所有的child的相加后的位置 int preLeft = getPaddingLeft(); int preTop = getPaddingTop(); //记录每一行的最高值 int maxHeight = 0; for (int i = 0; i < count; i++) { View child = getChildAt(i); MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams(); //r-l为当前容器的宽度。如果子view的累积宽度大于容器宽度,就换行。 if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) { //重置 preLeft = getPaddingLeft(); //要选择child的height最大的作为设置 preTop = preTop + maxHeight; maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin; } else { //不换行,计算最大高度 maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin); } //left坐标 int left = preLeft + params.leftMargin; //top坐标 int top = preTop + params.topMargin; int right = left + child.getMeasuredWidth(); int bottom = top + child.getMeasuredHeight(); //为子view布局 child.layout(left, top, right, bottom); //计算布局结束后,preLeft的值 preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin; } } }
Android radiobutton 图片与文字间距的问题
图一图二 图一和图二都是 radiobutton,在 Xml 里面的代码都是一样的 但放在两个手机里出现了不同的效果 也就是说 图一被设置过间距后仍然没有改变框与文字的间距 请问是什么问题造成的,代码:
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="阿尔巴尼亚"
android:layout_marginTop="5dp"
android:paddingLeft="100dp"
android:button="@xml/radiobutton"/>
Android RadioButton 图片位置与大小实例详解
Android RadioButton 图片位置与大小
Java:
rgGroup = (RadioGroup) findViewById(R.id.re_group); rbWeiHui = (RadioButton) findViewById(R.id.rb_wei_hui); rbAdd = (RadioButton) findViewById(R.id.rb_add); rbmine = (RadioButton) findViewById(R.id.rb_mine); //定义底部标签图片大小 Drawable drawableWeiHui = getResources().getDrawable(R.drawable.btn_tab_wei_hui_selector); drawableWeiHui.setBounds(0,69,69);//第一0是距左右边距离,第二0是距上下边距离,第三69长度,第四宽度 rbWeiHui.setCompoundDrawables(null,drawableWeiHui,null,null);//只放上面 Drawable drawableAdd = getResources().getDrawable(R.drawable.btn_tab_add_selector); drawableAdd.setBounds(0,168,120); rbAdd.setCompoundDrawables(drawableAdd,null); Drawable drawableRight = getResources().getDrawable(R.drawable.btn_tab_mine_selector); drawableRight.setBounds(0,69); rbmine.setCompoundDrawables(null,drawableRight,null); //初始化底部标签 rgGroup.check(R.id.rb_wei_hui);// 默认勾选首页,初始化时候让首页默认勾选
xml:
<RadioGroup android:id="@+id/re_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/app_bg_color" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_wei_hui"https://www.jb51.cc/tag/ott/" target="_blank">ottomTabStyle" android:layout_marginTop="5dp" android:drawabletop="@drawable/btn_tab_wei_hui_selector" android:textSize="12sp" android:text="xx" /> <RadioButton android:id="@+id/rb_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/app_bg_color" android:button="@null" android:drawabletop="@mipmap/ic_add_selected" android:gravity="center" android:paddingTop="10dip" /> <RadioButton android:id="@+id/rb_mine"https://www.jb51.cc/tag/ott/" target="_blank">ottomTabStyle" android:layout_marginTop="5dp" android:drawabletop="@drawable/btn_tab_mine_selector" android:textSize="12sp" android:text="xx" /> </RadioGroup>
selected:只写一个selected,其它模仿此
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@mipmap/ic_mine_selected" android:state_checked="true" /> <item android:drawable="@mipmap/ic_mine_normal" /> </selector>
style:共同的style-中间的是定制的,左右一个风格
<!-- 低栏RadioButton首页下面的标签的样式 --> <style name="BottomTabStyle"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_gravity">center_vertical</item> <item name="android:button">@null</item> <item name="android:padding">5dp</item> <item name="android:drawablePadding">3dp</item> <item name="android:textColor">@drawable/btn_tab_text_selector</item> <item name="android:layout_weight">1</item> <item name="android:gravity">center</item> <item name="android:layout_marginTop">5dp</item> </style>
效果:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
我们今天的关于android 自定义 radiobutton 文字颜色随选中状态而改变和radiobutton设置选中颜色的分享已经告一段落,感谢您的关注,如果您想了解更多关于018 Android 单选按钮 (RadioButton) 和复选框 (CheckBox) 的使用、Android RadioButton、Android radiobutton 图片与文字间距的问题、Android RadioButton 图片位置与大小实例详解的相关信息,请在本站查询。
本文标签: