动态设置UI

一,动态设置TextView的颜色

  /**
     * 设置TextView文字颜色
     * @param textView  需要改变颜色的view
     * @param normal    正常情况下,显示的颜色
     * @param selected  选择时,显示的颜色
     */

    public static void setSelectorColor(TextView textView,int normal,int selected){
        int[] colors =new int[] { normal, selected,normal};
        int[][] states =new int[3][];
        states[0] =new int[] { -android.R.attr.state_selected};
        states[1] =new int[] { android.R.attr.state_selected};
        states[2] =new int[] {};
        ColorStateList colorStateList =new ColorStateList(states,colors);
        textView.setTextColor(colorStateList);
    }

二,动态设置TextView的drawable

 TextView mWifiReconnectImage;
 Drawable drawable = null;
 if (themes.equals("blueSky")){
    drawable = getResources().getDrawable(R.drawable.video_reconnect_select_blue);
 }else {
    drawable = getResources().getDrawable(R.drawable.video_reconnect_select);
 }
 drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
 mWifiReconnectImage.setCompoundDrawables(null, drawable, null, null);

三,动态设置TextView的color(RGB)

 TextView mWifiReconnectImage;
 int colorInt = Color.parseColor("#FFFFFF");
 int[] int2Rgb = ColorUtil.int2Rgb(colorInt);
 mWifiReconnectImage.setTextColor(Color.rgb(int2Rgb[0], int2Rgb[1], int2Rgb[2]));

四,动态设置SeekBar的ProgressDrawable

 /**
     * 动态改变seekbar的ProgressDrawable
     * @param seekBar
     * @param color
     */
    public static void changeProgressDrawable(SeekBar seekBar, int color){
        LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
        int layers = layerDrawable.getNumberOfLayers();
        Drawable[] drawables = new Drawable[layers];
        for (int i = 0; i < layers; i++) {
            switch (layerDrawable.getId(i)) {
                // 如果是seekbar背景
                case android.R.id.background:
                    drawables[i] = layerDrawable.getDrawable(0);
                    break;
                // 如果是拖动条第一进度
                case android.R.id.progress:
                    //这里为动态的颜色值
                    drawables[i] = layerDrawable.getDrawable(1);
                    drawables[i].setColorFilter(color,SRC_ATOP);
                    drawables[i].setBounds(layerDrawable.getDrawable(0).getBounds());
                    break;
            }
        }
        seekBar.setProgressDrawable(new LayerDrawable(drawables));
        seekBar.invalidate();

    }

五,动态设置shape的颜色

 /**
     * 动态改变shape的颜色
     * @param imageView
     * @param color
     */
    public static void changeShapeColor(ImageView imageView, int color){
        GradientDrawable imageViewGrad = (GradientDrawable)imageView.getBackground();
        imageViewGrad.setStroke(1,color);
        imageViewGrad.setColor(color);

    }

六,动态设置TextView的图标

 /**
     * 设置TextView图标
     * @param textView
     * @paramd rawableNormal常态时的图片
     * @paramd rawableSelect选中时的图片
     */

    public static void setSelectorDrawable(TextView textView, Drawable drawableNormal, Drawable drawableSelect){
        StateListDrawable drawable =new StateListDrawable();
        drawable.addState(new int[]{android.R.attr.state_selected},drawableSelect);
        drawable.addState(new int[]{-android.R.attr.state_selected},drawableNormal);
        textView.setBackgroundDrawable(drawable);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值