android 自定义RadioButton 实现图标居中显示
2013-07-25 11:00
549 查看
源码
package com.fanli.android.view;
import com.fanli.android.apps.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;
/**
* @ClassName: MyRadioButton
* @Description: 自定义的 RadioButton
* @author xiang.shi
* @date 2013-7-25 上午10:45:15
*
*/
public class MyRadioButton extends RadioButton {
Drawable buttonDrawable;
public MyRadioButton(Context context) {
super(context);
}
public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CompoundButton, 0, 0);
buttonDrawable = a.getDrawable(1);
setButtonDrawable(android.R.color.transparent);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (buttonDrawable != null) {
buttonDrawable.setState(getDrawableState());
final int verticalGravity = getGravity()
& Gravity.VERTICAL_GRAVITY_MASK;
final int height = buttonDrawable.getIntrinsicHeight();
int y = 0;
switch (verticalGravity) {
case Gravity.BOTTOM:
y = getHeight() - height;
break;
case Gravity.CENTER_VERTICAL:
y = (getHeight() - height) / 2;
break;
}
int buttonWidth = buttonDrawable.getIntrinsicWidth();
int buttonLeft = (getWidth() - buttonWidth) / 2;
buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y
+ height);
buttonDrawable.draw(canvas);
}
}
}在布局文件中
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="horizontal" >
<com.example.viewpagedemo.MyRadioButton
android:id="@+id/radio0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:button="@drawable/home_press"
android:layout_weight="1"
android:checked="true"
android:text="" />
<com.example.viewpagedemo.MyRadioButton
android:id="@+id/radio1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:button="@drawable/home_press"
android:layout_weight="1"
android:text="" />
<com.example.viewpagedemo.MyRadioButton
android:id="@+id/radio2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:button="@drawable/home_press"
android:layout_weight="1"
android:text="" />
</RadioGroup>
setButtonDrawable(android.R.color.transparent);3.0以上必须用这个 3.0以下可用
代替
package com.fanli.android.view;
import com.fanli.android.apps.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;
/**
* @ClassName: MyRadioButton
* @Description: 自定义的 RadioButton
* @author xiang.shi
* @date 2013-7-25 上午10:45:15
*
*/
public class MyRadioButton extends RadioButton {
Drawable buttonDrawable;
public MyRadioButton(Context context) {
super(context);
}
public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CompoundButton, 0, 0);
buttonDrawable = a.getDrawable(1);
setButtonDrawable(android.R.color.transparent);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (buttonDrawable != null) {
buttonDrawable.setState(getDrawableState());
final int verticalGravity = getGravity()
& Gravity.VERTICAL_GRAVITY_MASK;
final int height = buttonDrawable.getIntrinsicHeight();
int y = 0;
switch (verticalGravity) {
case Gravity.BOTTOM:
y = getHeight() - height;
break;
case Gravity.CENTER_VERTICAL:
y = (getHeight() - height) / 2;
break;
}
int buttonWidth = buttonDrawable.getIntrinsicWidth();
int buttonLeft = (getWidth() - buttonWidth) / 2;
buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y
+ height);
buttonDrawable.draw(canvas);
}
}
}在布局文件中
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="horizontal" >
<com.example.viewpagedemo.MyRadioButton
android:id="@+id/radio0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:button="@drawable/home_press"
android:layout_weight="1"
android:checked="true"
android:text="" />
<com.example.viewpagedemo.MyRadioButton
android:id="@+id/radio1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:button="@drawable/home_press"
android:layout_weight="1"
android:text="" />
<com.example.viewpagedemo.MyRadioButton
android:id="@+id/radio2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:button="@drawable/home_press"
android:layout_weight="1"
android:text="" />
</RadioGroup>
setButtonDrawable(android.R.color.transparent);3.0以上必须用这个 3.0以下可用
android.R.id.empty
代替
相关文章推荐
- Android:自定义PopupMenu的样式(显示图标/设置RadioButton图标)
- Android编程实现自定义PopupMenu样式示例【显示图标与设置RadioButton图标】
- Android自定义TextView实现文字图片居中显示
- Android自定义TextView实现文字图片居中显示的方法
- Android:自定义PopupMenu的样式(显示图标/设置RadioButton图标)
- Android 自定义RadioButton实现
- android gridview布局,实现长按某一个,所有项都显示删除的图标
- Android - 自定义View 实现 文本吉他谱的 显示 实现
- 转Android APP安装后不在桌面显示图标的应用场景举例和实现方法
- Android自定义ImageView实现圆形控件显示
- Android应用图标在状态栏上显示实现原理
- Android 类似未读短信,电话图标显示数字效果如何实现的
- Android APP安装后不在桌面显示图标的应用场景举例和实现方法
- Android APP安装后不在桌面显示图标的应用场景举例和实现方法
- Android自定义组件,实现图片和文字同时显示
- Android自定义View设定到FrameLayout布局中实现多组件显示
- Android中Spinner下拉列表(使用自定义Adapter实现,显示数据取自联系人数据库)
- Android自定义LayoutManager实现表格,卡片显示效果
- android 自定义RadioButton(单选按钮)图标随便定.
- Android自定义Button结合PopupWindow和ListView实现顺序显示下拉菜单控件