Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android单选按钮图像_Android - Fatal编程技术网

Android单选按钮图像

Android单选按钮图像,android,Android,我有一个带有自定义单选按钮的radiogroup。图标是使用 rbFirst.setButtonDrawable(R.drawable.first); 但图标不在中心,如何修复? 我在xml文件和类内尝试了不同的属性,但没有效果。尝试使用setGravity(Gravity.CENTER) 或者,如果没有帮助,您将不得不从RadioButton派生一个新类并重写onDraw() 这是布局图 <?xml version="1.0" encoding="utf-8"?> <Rel

我有一个带有自定义单选按钮的radiogroup。图标是使用

rbFirst.setButtonDrawable(R.drawable.first);
但图标不在中心,如何修复? 我在xml文件和类内尝试了不同的属性,但没有效果。

尝试使用
setGravity(Gravity.CENTER)

或者,如果没有帮助,您将不得不从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>
最后,这里是一个attrs.xml文件,您需要将其放入res/values中,这样代码就可以获得平台定义的属性

<?xml version="1.0" encoding="utf-8"?>
<resources>    
     <declare-styleable name="CompoundButton">
        <attr name="android:button" />
    </declare-styleable>
</resources>

尝试使用
设置重力(Gravity.CENTER)

或者,如果没有帮助,您将不得不从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>
最后,这里是一个attrs.xml文件,您需要将其放入res/values中,这样代码就可以获得平台定义的属性

<?xml version="1.0" encoding="utf-8"?>
<resources>    
     <declare-styleable name="CompoundButton">
        <attr name="android:button" />
    </declare-styleable>
</resources>