身为一个Android开发人员,总是会遇到很多需求

直接上代码,代码里有注释,我使用的就是原生的radioGroup+radioButton

public class IndexActivity extends AppCompatActivity {    private RadioGroup index_rg_bottom;    private RadioButton index_rb_fragment_home, index_rb_fragment_living, index_rb_fragment_art, index_rb_fragment_near, index_rb_fragment_mine;    private List buttonDatas;    private List mySimpleTargets;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_index);        initView();        initRadioButtons();        initMySimpleTargets();        index_rg_bottom.setOnCheckedChangeListener(new myOnCheckedChangeListener());        index_rg_bottom.check(R.id.index_rb_fragment_home);    }    //按钮的图片加载    private void initLoadUrl() {        String path = "http://benyouhuifile.it168.com/forum/201208/17/1448124i6da4zzi06lry50.jpg";        String path2 = "https://cdn.pixabay.com/user/2014/05/07/00-10-34-2_96x96.jpg";        if (nPosition == oPosition) {            for (int i = 0; i < buttonDatas.size(); i++) {                if (nPosition == i) {                    Glide.with(this).load(path2).into(mySimpleTargets.get(i));                } else {                    Glide.with(this).load(path).into(mySimpleTargets.get(i));                }            }        } else {            Glide.with(this).load(path2).into(mySimpleTargets.get(nPosition));            Glide.with(this).load(path).into(mySimpleTargets.get(oPosition));        }    }    private int nPosition = 0;    private int oPosition = 0;    //redioGroup的监听,在radioButton改变时重新遍历底部按钮图片的加载    class myOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {        @Override        public void onCheckedChanged(RadioGroup group, int checkedId) {            switch (checkedId) {                case R.id.index_rb_fragment_home:                    nPosition = 0;                    break;                case R.id.index_rb_fragment_living:                    nPosition = 1;                    break;                case R.id.index_rb_fragment_art:                    nPosition = 2;                    break;                case R.id.index_rb_fragment_near:                    nPosition = 3;                    break;                case R.id.index_rb_fragment_mine:                    nPosition = 4;                    break;                default:                    nPosition = 0;                    break;            }            initLoadUrl();            oPosition = nPosition;        }    }    //初始化页面    private void initView() {        index_rg_bottom = findViewById(R.id.index_rg_bottom);        index_rb_fragment_home = findViewById(R.id.index_rb_fragment_home);        index_rb_fragment_living = findViewById(R.id.index_rb_fragment_living);        index_rb_fragment_art = findViewById(R.id.index_rb_fragment_art);        index_rb_fragment_near = findViewById(R.id.index_rb_fragment_near);        index_rb_fragment_mine = findViewById(R.id.index_rb_fragment_mine);    }    //buttonDatas添加按钮    private void initRadioButtons() {        buttonDatas = new ArrayList<>();        buttonDatas.add(index_rb_fragment_home);        buttonDatas.add(index_rb_fragment_living);        buttonDatas.add(index_rb_fragment_art);        buttonDatas.add(index_rb_fragment_near);        buttonDatas.add(index_rb_fragment_mine);    }    private void initMySimpleTargets() {        mySimpleTargets = new ArrayList<>();        for (RadioButton button : buttonDatas) {            //为每个按钮创建一个自定义SimpleTarget            mySimpleTargets.add(new MySimpleTarget(button));        }    }

上面的就是java代码了,不过还少了一个自定义方法类 MySimpleTarget.class

public class MySimpleTarget extends SimpleTarget {    private RadioButton radioButton;    public MySimpleTarget(RadioButton radioButton) {        this.radioButton = radioButton;    }    @Override    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {        resource.setBounds(0, 0, 69, 69);        radioButton.setCompoundDrawables(null, resource, null, null);    }}
resource.setBounds(0, 0, 69, 69);

四个参数分别代表在图片的左上角和右下角

radioButton.setCompoundDrawables(null,null,null,null);

四个参数分别代表在图片radioButton的左上右下

<?xml version="1.0" encoding="utf-8"?>                                                                                        

 

android:textColor="@drawable/index_rb_bottom_text_color_selector"

这个东西自己去写吧,网上有很多的,button的样式可以抽出来写一个style

加油吧,小菜

更多相关文章

  1. Android下载网络图片到本地
  2. Android学习札记22:ThumbnailUtils
  3. Dialog设置全屏
  4. Android(安卓)Studio 快捷键
  5. android 让TextView自带滚动条
  6. 打开Android(安卓)Studio报错"required plugin “Android(安卓)S
  7. android延续按两次返回退出程序(完整代码)
  8. ImageView.setScalType参数详解
  9. Android(安卓)取出 图片以字节形式存放到数据库的数据,并将图片显

随机推荐

  1. Android应用程序签名验证过程分析
  2. Android中各种JAVA包的功能描述
  3. Android技术架构演进
  4. 基于Eclipse的Android开发(一)——创建第一
  5. Android操作Excel文件的功能实现
  6. Android开发中的drawable文件:shape、laye
  7. Android设备扫描机制详解
  8. Android(安卓)Handler Looper Message工
  9. android键盘隐藏
  10. 如何在android 中编译alsa-utils工具