android RadioButton按钮可绘制重力

Sha*_*dul 26 android center radio-button drawable

我正在动态生成RadioButtons

RadioButton radioButton=new RadioButton(context);  

LayoutParams layoutParams=new LayoutParams(radioWidth,radioHeight);
layoutParams.gravity=Gravity.CENTER;

radioButton.setLayoutParams(layoutParams);
radioButton.setGravity(Gravity.CENTER);

BitmapDrawable bitmap = ((BitmapDrawable)drawableResource);
bitmap.setGravity(Gravity.CENTER);

radioButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.itabs_radio));
radioButton.setButtonDrawable(bitmap);
Run Code Online (Sandbox Code Playgroud)

你可以看到我拼命想把按钮的重力设置为中心,但没有理由它总是居中和左对齐,继承人的原因 - 安卓单选按钮的默认样式:

<style name="Widget.CompoundButton">
<item name="android:focusable">true</item> 
<item name="android:clickable">true</item>
<item name="android:textAppearance">?android:attr/textAppearance</item> 
<item name="android:textColor">?android:attr/textColorPrimaryDisableOnly</item> 
<item name="android:gravity">center_vertical|left</item> 
</style>

<style name="Widget.CompoundButton.RadioButton">
<item name="android:background">@android:drawable/btn_radio_label_background</item> 
<item name="android:button">@android:drawable/btn_radio</item> 
</style>
Run Code Online (Sandbox Code Playgroud)

有什么方法可以将按钮对齐到中心吗?

Reu*_*ton 69

根据CompoundButton.onDraw()源代码,它总是左对齐.

(注意行buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height);)

您必须从中派生一个新类RadioButton并重写onDraw().

示例后来添加:

好的,所以这就是你做的.首先,这是一个布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<org.test.TestProj.RadioButtonCenter
    android:id="@+id/myview"
    android:layout_width="fill_parent" 
    android:layout_height="100dp" 
    android:layout_centerInParent="true"
    android:text="Button test"
    />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

其次,这是自定义绘图RadioButtonCenter:

package org.test.TestProj;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

public class RadioButtonCenter extends RadioButton {

    public RadioButtonCenter(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);
    }
    Drawable buttonDrawable;


     @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);
            }
        }   
}
Run Code Online (Sandbox Code Playgroud)

最后,这是一个需要放入res/valuesattrs.xml文件,以便代码可以获得平台定义的属性.

<?xml version="1.0" encoding="utf-8"?>
<resources>    
     <declare-styleable name="CompoundButton">
        <attr name="android:button" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢一群Shardul.请允许我指出您的版本具有自定义构造函数,不正确使用属性,因此不能用作RadioButton的替代品.没关系.那是我最后一次遇到那种麻烦. (8认同)
  • 当使用`compile'c​​om.android.support:design:23.0.1'``a.getDrawable(1);`返回null.使用`a.getDrawable(R.styleable.CompoundButton_android_button);`虽然对我有用. (6认同)
  • 而不是android.R.id.empty,定义透明颜色并改为使用R.color.transparent,否则,抛出InflateException但它的真正原因是`android.content.res.Resources $ NotFoundException:来自可绘制资源ID的文件#0x1020004` (3认同)
  • 另外,请允许我指出我*做了*回答你的问题:你继承了RadioButton. (2认同)

hoo*_*oot 7

简单的解决方案,您可以向RadioButton添加背景,或设置background ="@ null",.

<RadioButton
                android:id="@+id/cp_rd_btn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"/>
Run Code Online (Sandbox Code Playgroud)

更新:

<RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@null"
                    android:button="@null"
                    android:drawableTop="@drawable/account_coolme_selector"
                    android:gravity="center" />

                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@null"
                    android:button="@null"
                    android:drawableTop="@drawable/account_qq_selector"
                    android:gravity="center"
                    />
            </RadioGroup>
Run Code Online (Sandbox Code Playgroud)