RadioGroup 和RadioButton制作Tab修改RadioButton的大小

使用RadionGroup做Tab时,调整RadioButton的图片大小有两种方式:
设置前:
RadioGroup 和RadioButton制作Tab修改RadioButton的大小_第1张图片
1.直接修改图片的大小再赋值到RadioButton
布局文件:

    "@+id/main_rg_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/bg_auction_price_add"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:orientation="horizontal">

        "@+id/main_rb_home"
            style="@style/HomeRadioButton"
            android:drawableTop="@drawable/tab_selector_home"
            android:text="@string/pagehome" />

        "@+id/main_rb_message"
            style="@style/HomeRadioButton"
            android:drawableTop="@drawable/tab_selector_msg"
            android:text="@string/Information" />
   </RadioGroup>

代码设置

Resources resources = getResources();
Drawable drawable_home = resources.getDrawable(R.drawable.tab_selector_home);
Drawable drawable_msg = resources.getDrawable(R.drawable.tab_selector_msg);

//第一0是距左右边距离,第二0是距上下边距离,第三80长度,第四80宽度
drawable_home.setBounds(0,0,80,80);
drawable_msg.setBounds(0,0,80,80);
//图片只放上面
mRadioButtonHome.setCompoundDrawables(null,drawable_home,null,null);
mRadioButtonMessage.setCompoundDrawables(null,drawable_msg,null,null);

2.通过设置RadioGroup的布局设置图片的大小

RadioGroup mRadioGroup;
float density = getResources().getDisplayMetrics().density;//获取屏幕密度
RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams((int) (8 * density), (int) (8 * density));//设置RadioButton的大小
int margin = (int) (6 * density);
params_rb.setMargins(margin, margin, margin, margin);//设置RadioButton之间的距离
for (int i = 0; i < count; i++) {
     RadioButton rdbtn = (RadioButton) inflater.inflate(R.layout.item_radiobutton_looper, null);
     rdbtn.setId(i);
     mRadioGroup.addView(rdbtn, params_rb);
  }

设置后
RadioGroup 和RadioButton制作Tab修改RadioButton的大小_第2张图片

你可能感兴趣的:(Android,基础知识)