在RadioButton使用中,需要放入图片,设置
android:button="@null"
android:drawableTop="@drawable/tab_homepage"
android:drawablePadding=“3dip”
这几个属性配合使用。
<RadioGroup
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:layout_marginBottom="5dip"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:orientation="horizontal">
<RadioButton
android:id="@+id/homepageRB"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:drawablePadding="3dip"
android:gravity="center"
android:textSize="17sp"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/tab_homepage"
android:text="首页" />
<View
android:layout_width="1dip"
android:background="@color/colorA4"
android:layout_height="match_parent"/>
<RadioButton
android:id="@+id/noticeRB"
android:layout_width="0dp"
android:layout_weight="1"
android:drawablePadding="3dip"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="17sp"
android:button="@null"
android:drawableTop="@drawable/tab_notice"
android:text="通知" />
</RadioGroup>
但是图片明显太小了,如何设置图片大小
XML中无法直接设置图片的大小,通过Java代码实现,代码如下:
Drawable homepageDrawable= getResources().getDrawable(R.drawable.tab_homepage);
homepageDrawable.setBounds(0, 0, 69, 69);//参数1是距左右边距离,参数2是距上下边距离,参数3长度,参数4宽度
homepageRB.setCompoundDrawables(null, homepageDrawable, null, null);//只放上面
Drawable noticeDrawable = getResources().getDrawable(R.drawable.tab_notice);
noticeDrawable.setBounds(0, 0, 69, 69);
noticeRB.setCompoundDrawables(null, noticeDrawable, null, null);
setBounds方法,宽高单位是px,可以根据美工设计,赋值实际值,dp转px值填入,这时候就可以动态调整图片大小了。