首页 > 解决方案 > 使用 setAlpha 设置布局背景的 alpha 不起作用

问题描述

我有点困惑。我有这个 :

this.opacitySeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            // updating opacity of layout
            RelativeLayout ledColor;
            for(int index = 0; index < selectedLedsIndices.size(); index++) {
                ledColor = (RelativeLayout)ledsLayout.getChildAt(index).findViewById(R.id.led_color);
                ledColor.setAlpha(((float)i) / 255); 
            }

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

我正在尝试使用搜索栏的进度值更改布局的不透明度。seekbar 的值从 à 变为 255。但是在运行时,布局的不透明度不会改变。我没有得到任何错误跟踪。它只是不想改变。怎么样?

我在 api 24 上运行应用程序(我的应用程序应该针对 api 从 19 到 27)。

标签: androidandroid-layoutalphaandroid-seekbar

解决方案


利用ledColor.getBackground().setAlpha(...)


推荐阅读