美文网首页
Android 为radiogroup代码动态加入radiobu

Android 为radiogroup代码动态加入radiobu

作者: 笨coco | 来源:发表于2016-05-05 13:50 被阅读2070次

    public void addRadioGropItem(){

    DisplayMetrics dm = new DisplayMetrics();

    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int indicatorWidth = dm.widthPixels / arr.length;

    mRadioGroup.removeAllViews();

    for(int i=0;i<5;i++){

    RadioButton rb = (RadioButton)this.getLayoutInflater().inflate(R.layout.activity_main_radiogroup_item, null);

    rb.setId(i);

    rb.setText(arr[i]);

    Drawable drawable=getResources().getDrawable(R.drawable.btn_home);

    int w = drawable.getIntrinsicWidth();

    int h=drawable.getIntrinsicHeight();

    /////这一步必须要做,否则不会显示.

    drawable.setBounds(0, 0, w, h);

    rb.setCompoundDrawables(null,drawable,null,null);

    rb.setPadding(0,20, 0, 0);

    //rb.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null);

    //rb.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.btn_home,0,0);

    rb.setLayoutParams(new LayoutParams(indicatorWidth,LayoutParams.MATCH_PARENT));

    mRadioGroup.addView(rb);

    }

    }

    Radiobutton添加内容有三种方式:

    1. Drawable drawable= getResources().getDrawable(R.drawable.btn_home);

    int w = drawable.getIntrinsicWidth();

    int h=drawable.getIntrinsicHeight();

    /////这一步必须要做,否则不会显示.

    drawable.setBounds(0, 0, w, h);

    rb.setCompoundDrawables(null,drawable,null,null);

    rb.setPadding(0,20, 0, 0);

    2. rb.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null);

    3. rb.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.btn_home,0,0);

    相关文章

      网友评论

          本文标题:Android 为radiogroup代码动态加入radiobu

          本文链接:https://www.haomeiwen.com/subject/koedrttx.html