You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

第二种自定义一个MyRadioButton。原理跟第一种是一样的直接上代码吧。
第一步在attrs文件夹下面定义一个图片引用的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
? ? <declare-styleable name="MyRadioButton">
? ? ? ? <attr name="drawableTop" format="reference" />
? ? </declare-styleable>
</resources>
第二步定义一个MyRadioButton类继承原生的RadioButton
public class MyRadioButton extends RadioButton {
private Drawable drawable;
public MyRadioButton(Context context) {
super(context);
}
public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);//获取我们定义的属性
drawable = typedArray.getDrawable(R.styleable.MyRadioButton_drawableTop);
drawable.setBounds(0, 0, 60, 60);
setCompoundDrawables(null, drawable, null, null);
}
}
第三步xml中使用它
<com.myapp.view.MyRadioButton
?????android:id="@+id/rb1"
?????android:layout_width="wrap_content"
?????android:layout_height="wrap_content"
?????android:layout_gravity="center"
?????android:layout_weight="1"
?????android:button="@null"
?????android:gravity="center"
?????android:text="哈哈1"
? ? ?attr:drawableTop="@drawable/单个图片或者选择器图片" />
<com.myapp.view.MyRadioButton
?????android:id="@+id/rb2"
?????android:layout_width="wrap_content"
?????android:layout_height="wrap_content"
?????android:layout_gravity="center"
?????android:layout_weight="1"
?????android:button="@null"
?????android:gravity="center"
?????android:text="哈哈2"
? ? ?attr:drawableTop="@drawable/-----" />
<com.myapp.view.MyRadioButton
?????android:id="@+id/rb3"
?????android:layout_width="wrap_content"
?????android:layout_height="wrap_content"
?????android:layout_gravity="center"
?????android:layout_weight="1"
?????android:button="@null"
?????android:gravity="center"
?????android:text="哈哈3"
? ? ?attr:drawableTop="@drawable/----" />
运行:完美!
需要主意的是要是在RadioGroup中默认选中第几个但是点击后不互斥这是因为没有给RadioButton写id。
另一种:
private void initView(){
Drawable drawable_news = getResources().getDrawable(R.drawable.selector_home_rbtn_news);
//当这个图片被绘制时,给他绑定一个矩形 ltrb规定这个矩形
drawable_news.setBounds(0, 0, 50, 50); //设置图片在文字的哪个方向
rbtn_News.setCompoundDrawables(null, drawable_news, null, null);
// 定义底部标签图片大小和位置
Drawable drawable_live = getResources().getDrawable(R.drawable.selector_home_rbtn_live);
//当这个图片被绘制时,给他绑定一个矩形 ltrb规定这个矩形
drawable_live.setBounds(0, 0, 50, 50);
//设置图片在文字的哪个方向
rbtn_Live.setCompoundDrawables(null, drawable_live, null, null);
//定义底部标签图片大小和位置
Drawable drawable_tuijian = getResources().getDrawable(R.drawable.selector_home_rbtn_tuijian);
//当这个图片被绘制时,给他绑定一个矩形 ltrb规定这个矩形
drawable_tuijian.setBounds(0, 0, 50, 50);
//设置图片在文字的哪个方向
rbtn_Tuijian.setCompoundDrawables(null, drawable_tuijian, null, null);
//定义底部标签图片大小和位置
Drawable drawable_me = getResources().getDrawable(R.drawable.selector_home_rbtn_me);
//当这个图片被绘制时,给他绑定一个矩形 ltrb规定这个矩形
drawable_me.setBounds(0, 0, 50, 50);
//设置图片在文字的哪个方向
rbtn_Me.setCompoundDrawables(null, drawable_me, null, null);
}