Radiogroup RadioButton 的drawableto 背景图片大小修改

方法一:将图片放到不同的资源文件夹下,生成的图片会不一样。简单粗暴。



方法二:

java:

[java]  view plain  copy
  1. rgGroup = (RadioGroup) findViewById(R.id.re_group);  
  2.         rbWeiHui = (RadioButton) findViewById(R.id.rb_wei_hui);  
  3.         rbAdd = (RadioButton) findViewById(R.id.rb_add);  
  4.         rbMine = (RadioButton) findViewById(R.id.rb_mine);  
  5.   
  6.         //定义底部标签图片大小  
  7.         Drawable drawableWeiHui = getResources().getDrawable(R.drawable.btn_tab_wei_hui_selector);  
  8.         drawableWeiHui.setBounds(006969);//第一0是距左右边距离,第二0是距上下边距离,第三69长度,第四宽度  
  9.         rbWeiHui.setCompoundDrawables(null, drawableWeiHui, nullnull);//只放上面  
  10.   
  11.         Drawable drawableAdd = getResources().getDrawable(R.drawable.btn_tab_add_selector);  
  12.         drawableAdd.setBounds(00168120);  
  13.         rbAdd.setCompoundDrawables(drawableAdd, nullnullnull);  
  14.   
  15.         Drawable drawableRight = getResources().getDrawable(R.drawable.btn_tab_mine_selector);  
  16.         drawableRight.setBounds(006969);  
  17.         rbMine.setCompoundDrawables(null, drawableRight, nullnull);  
  18.   
  19.         //初始化底部标签  
  20.         rgGroup.check(R.id.rb_wei_hui);// 默认勾选首页,初始化时候让首页默认勾选  

xml:

[html]  view plain  copy
  1. <RadioGroup  
  2.         android:id="@+id/re_group"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:background="@color/app_bg_color"  
  6.         android:orientation="horizontal" >  
  7.   
  8.         <RadioButton  
  9.             android:id="@+id/rb_wei_hui"  
  10.             style="@style/BottomTabStyle"  
  11.             android:layout_marginTop="5dp"  
  12.             android:drawableTop="@drawable/btn_tab_wei_hui_selector"  
  13.             android:textSize="12sp"  
  14.             android:text="xx" />  
  15.   
  16.         <RadioButton  
  17.             android:id="@+id/rb_add"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:background="@color/app_bg_color"  
  21.             android:button="@null"  
  22.             android:drawableTop="@mipmap/ic_add_selected"  
  23.             android:gravity="center"  
  24.             android:paddingTop="10dip" />  
  25.   
  26.         <RadioButton  
  27.             android:id="@+id/rb_mine"  
  28.             style="@style/BottomTabStyle"  
  29.             android:layout_marginTop="5dp"  
  30.             android:drawableTop="@drawable/btn_tab_mine_selector"  
  31.             android:textSize="12sp"  
  32.             android:text="xx" />  
  33.     </RadioGroup>  

selected:只写一个selected,其它模仿此

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <item android:drawable="@mipmap/ic_mine_selected" android:state_checked="true" />  
  5.     <item android:drawable="@mipmap/ic_mine_normal" />  
  6.   
  7.   
  8. </selector>  

style:共同的style-中间的是定制的,左右一个风格

[html]  view plain  copy
  1. <!-- 低栏RadioButton首页下面的标签的样式  -->  
  2.    <style name="BottomTabStyle">  
  3.        <item name="android:layout_width">wrap_content</item>  
  4.        <item name="android:layout_height">wrap_content</item>  
  5.        <item name="android:layout_gravity">center_vertical</item>  
  6.        <item name="android:button">@null</item>  
  7.        <item name="android:padding">5dp</item>  
  8.        <item name="android:drawablePadding">3dp</item>  
  9.        <item name="android:textColor">@drawable/btn_tab_text_selector</item>  
  10.        <item name="android:layout_weight">1</item>  
  11.        <item name="android:gravity">center</item>  
  12.        <item name="android:layout_marginTop">5dp</item>  
  13.    </style>  

效果:



  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是的,可以通过RadioGroup获取选中的RadioButton。可以通过以下步骤实现: 1. 在布局文件中添加RadioGroupRadioButton控件,例如: ``` <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" /> </RadioGroup> ``` 2. 在Activity或Fragment中获取RadioGroup对象,并设置监听器,例如: ``` RadioGroup radioGroup = findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = findViewById(checkedId); if (radioButton != null) { String text = radioButton.getText().toString(); // Do something with the selected text } } }); ``` 在监听器的回调方法中,我们可以通过checkedId获取选中的RadioButton的id,然后通过findViewById方法获取该RadioButton对象,最后通过该对象的getText方法获取选中的文本内容。 注意,如果RadioGroup中没有选中任何RadioButton时,checkedId的值为-1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值