Make RelativeLayout checkable

后端 未结 4 1120
长发绾君心
长发绾君心 2021-01-03 04:53

I try to write my own CheckBox using RelativeLayout with TextView and FrameLayout (with selector on background) inside.

4条回答
  •  走了就别回头了
    2021-01-03 05:42

    Nik's answer is partially right for me, you need added android.R.attr.state_checkable into the STATE_CHECKABLE array, and set the corresponding lenth (2), or else it fails to work.

    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked, android.R.attr.state_checkable};
    
    @Override
    protected int[] onCreateDrawableState(int extraSpace)
    {
        int[] result = super.onCreateDrawableState(extraSpace + CHECKED_STATE_SET.length);
        if(mChecked) mergeDrawableStates(result, CHECKED_STATE_SET);
        return result;
    }
    

提交回复
热议问题