RadioButton的图片大小设置

在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值填入,这时候就可以动态调整图片大小了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值