身为一个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 取出 图片以字节形式存放到数据库的数据,并将图片显示
  3. android图片闪烁或帧动画
  4. Android 实现在Java代码中修改UI界面,并修改界面
  5. JS调用Java代码(Android)
  6. 【Android】Android Layout Binder——根据layout布局文件自动生
  7. Android应用程序进程启动过程的源代码分析(2)
  8. Android 的 Button 按钮实现的两种方式
  9. Android源代码编译

随机推荐

  1. Win7+Eclipse下Android开发环境配置
  2. Android(安卓): GestureDetector手势检测
  3. Ios与Android有趣的对比
  4. [置顶] android 自定义Toast,可设定显示时
  5. Android中Bitmap.createBitmap重载方总结
  6. Android架构组件(二)——LiveData
  7. Android(安卓)关于videoview 弹窗 “抱歉
  8. Android(安卓)Launcher抽屉类SlidingDraw
  9. Android开发指南目录
  10. Android操作framebuffer[zz]