Allen Lu~ 2015-11-10 02:42 采纳率: 50%
浏览 1914
已结题

自定义RadioButton点击问题,求解决

点击前
点击后
调了好久。。 求解决 代码如下~

布局文件如下:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/rg_leftMenu"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <cn.vko.teacher.base.MyRadioButton
                style="@style/leftMenuPrperty"
                android:layout_gravity="center"
                android:clickable="true"
                android:text="@string/MyLesson"
                myradio:drawableSize="30dp"
                myradio:drawableTop="@drawable/test" />

            <cn.vko.teacher.base.MyRadioButton
                style="@style/leftMenuPrperty"
                android:clickable="true"
                android:text="@string/Task"
                app:drawableSize="30dp"
                myradio:drawableTop="@drawable/iconfont_zuoyeguanli"
                tools:ignore="SpUsage" />

            <cn.vko.teacher.base.MyRadioButton
                style="@style/leftMenuPrperty"
                android:clickable="true"
                android:text="@string/Contacts"
                myradio:drawableSize="30dp"
                myradio:drawableTop="@drawable/iconfont_tongxunlu"
                tools:ignore="SpUsage" />
        </RadioGroup>
    </LinearLayout>

drawable的xml:

<item android:drawable="@drawable/asd1" android:state_checked="true" />
<item android:drawable="@drawable/iconfont_kecheng" android:state_checked="false"/>

重写RadioButton代码如下:

public class MyRadioButton extends RadioButton {

private int mDrawableSize;// xml文件中设置的大小


public MyRadioButton( Context context ) {
    this(context, null, 0);
}


public MyRadioButton( Context context, AttributeSet attrs ) {
    this(context, attrs, 0);
}


public MyRadioButton( Context context, AttributeSet attrs, int defStyle ) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);

    int n = a.getIndexCount();
    for ( int i = 0 ; i < n ; i++ ) {
        int attr = a.getIndex(i);
        switch ( attr ) {
            case R.styleable.MyRadioButton_drawableSize :
                mDrawableSize = a.getDimensionPixelSize(R.styleable.MyRadioButton_drawableSize, 50);
                Log.i("MyRadioButton", "mDrawableSize:" + mDrawableSize);
                break;
            case R.styleable.MyRadioButton_drawableTop :
                drawableTop = a.getDrawable(attr);
                break;
            case R.styleable.MyRadioButton_drawableBottom :
                drawableRight = a.getDrawable(attr);
                break;
            case R.styleable.MyRadioButton_drawableRight :
                drawableBottom = a.getDrawable(attr);
                break;
            case R.styleable.MyRadioButton_drawableLeft :
                drawableLeft = a.getDrawable(attr);
                break;
            default:
                break;
        }
    }
    a.recycle();

    setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);

}


@Override
public void setCompoundDrawablesWithIntrinsicBounds( Drawable left, Drawable top, Drawable right, Drawable bottom ) {

    if ( left != null ) {
        left.setBounds(0, 0, mDrawableSize, mDrawableSize);
    }
    if ( right != null ) {
        right.setBounds(0, 0, mDrawableSize, mDrawableSize);
    }
    if ( top != null ) {
        top.setBounds(0, 0, mDrawableSize, mDrawableSize);
    }
    if ( bottom != null ) {
        bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);
    }
    setCompoundDrawables(left, top, right, bottom);
}


@Override
protected void onDraw( Canvas canvas ) {
    Drawable[] drawables = getCompoundDrawables();
    if ( drawables != null ) {
        Drawable drawableTop = drawables[1];
        if ( drawableTop != null ) {
            float textHeight = measureHeight(getText().toString());
            int drawablePadding = getCompoundDrawablePadding();
            int drawableHeight = drawableTop.getIntrinsicHeight();
            float bodyHeight = textHeight + drawableHeight + drawablePadding;
            setPadding(0, (int) (getHeight() - bodyHeight), 0, 20);
            canvas.translate(0, 0 - (getHeight() - bodyHeight) / 2);
        }
    }
    super.onDraw(canvas);
}


// 获取文本高度
public int measureHeight( @SuppressWarnings( "unused" ) String text ) {
    Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();

    return (int) (fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading);
}

}

  • 写回答

1条回答 默认 最新

  • sanxiaochengyu 2015-11-10 06:21
    关注

    兄弟,你对图片进行了缩放
    所以出现了点问题
    onDraw中改一下,
    获取图片高度

                        int drawableHeight = 0;
                    if (drawableTop.getBounds().height() > 0) {
                        drawableHeight = drawableTop.getBounds().height();//获取图片缩放后高度
                    } else {
                        drawableHeight = drawableTop.getIntrinsicHeight();//获取图片实际高度
                    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型