RadioButton 图片的动态处理

场景:当我们采用RadioButton作为底部导航按钮的时候,我们会给RadioButton设置DrwableTop,有时候你会发现我们的切图或许尺寸不太符合,这时我们可以采用代码动态的处理图片尺寸。
1.首先看看我们xml布局的效果:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/rg_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <RadioButton
            android:gravity="center"
            android:id="@+id/rb_page"
            android:drawableTop="@drawable/hiyuanquanyi"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:text="首页"
            android:textSize="20sp" />

        <RadioButton
            android:gravity="center"
            android:id="@+id/rb_mall"
            android:drawableTop="@drawable/hiyuanquanyi"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:text="商城"
            android:textSize="20sp" />
        <RadioButton
            android:gravity="center"
            android:id="@+id/rb_me"
            android:drawableTop="@drawable/hiyuanquanyi"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:text="我的"
            android:textSize="20sp" />
    </RadioGroup>

</RelativeLayout>
image.png

看见效果瞬间崩溃了吧。。。接下来我们就处理图片:

package com.example.zhangbiao.myapplication;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    private RadioButton mRbMe;
    private RadioButton mRbMall;
    private RadioButton mRbPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        initData();
    }

    private void initUI() {
        mRbMe = findViewById(R.id.rb_me);
        mRbMall = findViewById(R.id.rb_mall);
        mRbPage = findViewById(R.id.rb_page);

    }

    private void initData() {
        RadioButton[] radioButton = new RadioButton[]{mRbMall, mRbMe, mRbPage};
        for (RadioButton rb : radioButton) {
            //获得每个drawable对象
            Drawable[] drawables = rb.getCompoundDrawables();
            // 获得一个矩阵大小    图片宽高的 2/5  大小可以自己设置
            Rect r = new Rect(0, 0, drawables[1].getMinimumWidth() * 2 / 5, drawables[1].getMinimumHeight() * 2 / 5);
            //给图片设置矩阵大小
            drawables[1].setBounds(r);
            // 设置给按钮的图片
            rb.setCompoundDrawables(null,drawables[1],null,null);

        }
    }
}

运行效果:


image.png

看到这里是不是心里舒服多了。

api相关参考:(不熟rect类的可以参考一下写得不错)https://blog.csdn.net/huangxiaominglipeng/article/details/21597575

总结:在项目中碰到了,总结 一下,希望对需要的人有所帮助,同时也是自己的记忆加深。。。。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,283评论 25 708
  • “啊!快来人啊!”新娘的妈妈林大娘惊慌失措的大吼道。“闺女,闺女啊,你这是咋啦?” 听到一声大叫的声音后,亲戚们纷...
    小马大杨阅读 491评论 1 3
  • 瘦人碗子阅读 561评论 0 1

友情链接更多精彩内容