xiaoyan_12 2012-10-31 05:41 采纳率: 25%
浏览 14557
已采纳

android中radio button居中对齐的问题

我使用以下代码动态的生成了 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);

由以上代码看出我一直在设置按钮的位置,想把它们放在布局的中间。但是不知道什么原因,按钮可以排列在中间,但是左对齐。以下是android中radio button的默认格式:

<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>

我用什么方法可以把所有的按钮按竖排排列,并都居中对齐呢?

  • 写回答

2条回答 默认 最新

  • O_Casper_ne 2012-11-01 03:12
    关注

    1.首先先创建一个布局:

    <?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>
    

    2.然后再添加自定义的RadioButtonCenter

    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.id.empty);
        }
        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);
                }
            }   
    }
    

    3.最后,你应该在res/values中添加 attrs.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>    
         <declare-styleable name="CompoundButton">
            <attr name="android:button" />
        </declare-styleable>
    </resources>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于温度改变石墨烯介电性能(关键词-介电常数)
  • ¥150 HDMI分路器LT86102 的输出在890MHz频点处EMC超标8DB
  • ¥15 druid(相关搜索:数据库|防火墙)
  • ¥15 大一python作业
  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥60 如何鉴定微信小程序数据被篡改过
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥20 手机截图相片分辨率降低一半