最近同事做界面时, 底部栏用RadioButton+FragmentTabhost时, 碰到个问题: 在底部的RadioGroup里面设置了5个RadioButton后, 在模拟机上跑,界面是正常的, 放在自己1080P的手机跑, 界面也正常. 然后放在720P时跑,底部栏RadioGroup就偏移了. 然后在电脑建立个800*480的模拟器跑, 界面也正常.感觉有点邪门, 来问我了.然后看了下这代码满满的布局文件, 也没啥大问题. 最后调试了下, 分别给各个RadioButton设置了背景Background属性后, 就正常了.
这里就是他的XML代码(已按照上面设置了Background属性)
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:gravity="center"
android:background="@color/white" >
<RadioButton
android:id="@+id/tab_fm_index"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableTop="@drawable/btn_btom_main_sele"
android:button="@null"
android:text="@string/homepage_string"
android:textColor="@color/textcolor_ragp"
android:textSize="13sp"
android:gravity="center"
android:singleLine="true"
android:checked="true"
android:background="@color/transparent"
/>
<RadioButton
android:id="@+id/tab_fm_forum"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableTop="@drawable/btn_btom_forum_sele"
android:button="@null"
android:text="@string/forum_string"
android:textColor="@color/textcolor_ragp"
android:textSize="13sp"
android:gravity="center"
android:singleLine="true"
android:background="@color/transparent"
/>
<RadioButton
android:id="@+id/tab_fm_push"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:drawableBottom="@drawable/btn_btom_push_1"
android:button="@null"
android:text=""
android:textColor="@color/textcolor_ragp"
android:textSize="0sp"
android:gravity="center"
android:singleLine="true"
android:background="@color/transparent"
/>
<RadioButton
android:id="@+id/tab_fm_msg"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableTop="@drawable/btn_btom_msg_sele"
android:button="@null"
android:text="@string/message_string"
android:textColor="@color/textcolor_ragp"
android:textSize="13sp"
android:gravity="center"
android:singleLine="true"
android:background="@color/transparent"
/>
<RadioButton
android:id="@+id/tab_fm_mine"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableTop="@drawable/btn_btom_mine_sele"
android:button="@null"
android:text="@string/mine_string"
android:textColor="@color/textcolor_ragp"
android:textSize="13sp"
android:gravity="center"
android:singleLine="true"
android:background="@color/transparent"
/>
</RadioGroup>
这里贴上正常和不正常的截图.
这是还没设置背景属性时,在720P真机上运行时, 底部RadioGroup偏移的截图
这是还没设置背景属性时,在800*480模拟机上运行时, 底部RadioGroup正常的截图
下面是添加背景属性后正常的截图:
小结:
用原生RadioButton的时候就会发现, 即使是很简陋的一个"点"的选择, 如果设置了wrap_content属性, 其实这个RadioButton组件还是挺占位置的, 周围有不少空白的多余"地儿".即使人为设置高宽,也感觉无法将该组件变到非常理想. 而设置了背景后, 组件直接受铺满背景的draw影响, 高宽大小的设置变得有效了,当然, 如果单纯是为了满足这点, 将背景设置为@color/transparent 是最佳的.