如何通过Helper类更改不同类型的布局的颜色

时间:2019-07-16 15:44:26

标签: android android-studio

我正在使用下面的代码来更改不同布局的背景色,以减少我想使用辅助类的代码。怎么做到呢?我应该用什么变量来称呼课程?

如果我想为每种布局使用不同的背景色,应该怎么称呼课程?

    final NavigationView navigationView =(NavigationView)findViewById(R.id.navigation_view);

    ShapeDrawable.ShaderFactory sf1 = new ShapeDrawable.ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            LinearGradient lg1 = new LinearGradient(0, 0, height, width,
                    new int[]{
                            Color.rgb(238,162,173),
                            Color.rgb(70, 130, 160),
                            Color.rgb(99, 184, 223),
                            Color.rgb(205,198,115),
                            Color.rgb(176, 226, 155),

                    },
                    new float[]{0,.1f,.51f,.52f,.11f}, Shader.TileMode.REPEAT);
            return lg1;
        }
    };
    PaintDrawable p = new PaintDrawable();
    p1.setShape(new RectShape());
    p1.setShaderFactory(sf1);
    p1.setCornerRadius(10.8f);
    navigationView.setBackground((Drawable)p1);




   final LinearLayout layout =(LinearLayout)findViewById(R.id.ll);

    ShapeDrawable.ShaderFactory sf2 = new ShapeDrawable.ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            LinearGradient lg2 = new LinearGradient(0, 0, height, width,
                    new int[]{
                            Color.rgb(238,162,173),
                            Color.rgb(70, 130, 160),
                            Color.rgb(99, 184, 223),
                            Color.rgb(205,198,115),
                            Color.rgb(176, 226, 255),

                    },
                    new float[]{0,.1f,.51f,.52f,.11f}, Shader.TileMode.REPEAT);
            return lg2;
        }
    };
    PaintDrawable np = new PaintDrawable();
    p2.setShape(new RectShape());
    p2.setShaderFactory(sf2);
    p2.setCornerRadius(10.8f);
    layout.setBackground((Drawable)p2);

0 个答案:

没有答案