Android开发中如何改变RadioButton背景图片和文字的相对位置

先上效果图:

 
标签: <无>
 

代码片段(2)[全屏查看所有代码]

1. [图片] self_radion_button.png    

2. [代码][Java]代码     跳至 [2] [全屏预览]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
具体代码的实现:
 
 
.....
               <RadioButton
                        android:layout_width= "wrap_content"
                        android:layout_height= "wrap_content"
                        android:gravity= "center"
                        android:button= "@null"
                        android:paddingLeft= "5dp"
                        android:textSize= "10dip"
                        android:drawableBottom= "@android:drawable/btn_radio"
                        android:text= "@string/_str" />
                   
                     <RadioButton
                         android:layout_width= "wrap_content"
                         android:layout_height= "wrap_content"
                         android:gravity= "center"
                         android:button= "@null"
                         android:paddingLeft= "5dp"
                         android:textSize= "10dip"
                         android:drawableBottom= "@android:drawable/btn_radio"
                         android:text= "@string/_str" />
                     
                     <RadioButton
                         android:layout_width= "wrap_content"
                         android:layout_height= "wrap_content"
                         android:gravity= "center"
                         android:button= "@null"
                         android:textSize= "10dip"
                         android:paddingLeft= "1dp"
                         android:paddingRight= "5dp"
                         android:drawableBottom= "@android:drawable/btn_radio"
                         android:text= "@string/_str" />
.....
 
 
代码解释:
  
     android:button= "@null"
     这句是将RadioButton的背景图屏蔽。
  
     android:drawableBottom= "@android:drawable/btn_radio"
     此句用意是在原来图标的下边使用系统自带的btn_radio图片做为背景图
 
 
好了,就说这多了。
 
 
欲了解更多内容欢迎访问:http: //www.drovik.com/
 
 
补充内容:
 
  如果你想要替换掉系统提供的默认图片的话,你只需要做一下动作就可以了:
  
首先,在drawable目录下创建一个btn_selector.xml文件:
内容实例:
<?xml version= "1.0" encoding= "utf-8" ?>
<selector>
     <item android:state_checked= "true" android:state_window_focused= "false"
           android:drawable= "@drawable/btn_sort_check_on" />
     <item android:state_checked= "false" android:state_window_focused= "false"
           android:drawable= "@drawable/btn_sort_check_off" />
     <item android:state_checked= "false" android:drawable= "@drawable/btn_sort_check_off" />
     <item android:state_checked= "true" android:drawable= "@drawable/btn_sort_check_on" />
</selector>
  
然后,
  android:drawableBottom替换自己写的选择器。
  
android:drawableBottom= "@drawable/web_cam_radion_button_selector"
  
这样可以了。

转自“http://www.oschina.net/code/snippet_246750_16190”

你可能感兴趣的:(Android开发中如何改变RadioButton背景图片和文字的相对位置)