为imageview设置背景颜色不起作用

时间:2015-10-23 09:36:17

标签: android background-image android-imageview

我在点击时尝试更改ImageView背景(如Duolingo)

one

这是我的片段代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.frag_repeat, container, false);
    final int[] a1 = {0};
    final int[] a2 = {0};
    final int[] a3 = {0};
    final int[] a4 = {0};


    TypedArray itemsIcon = getResources().obtainTypedArray(R.array.nav_drawer_icons);



    ImageView wer1 = (ImageView) rootView.findViewById(R.id.imageView);

    ImageView wer2 = (ImageView) rootView.findViewById(R.id.imageView2);

    ImageView wer3 = (ImageView) rootView.findViewById(R.id.imageView23);

    ImageView wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
    TextView textView1 = (TextView) rootView.findViewById(R.id.textView711);


    textView1.setText(ss[i]);
    wer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a1[0] = 1;
            a2[0] = 0;
            a3[0] = 0;
            a4[0] = 0;
        }
    });
    wer2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a2[0] = 1;
            a1[0] = 0;
            a3[0] = 0;
            a4[0] = 0;
        }
    });
    wer3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a3[0] = 1;
            a2[0] = 0;
            a1[0] = 0;
            a4[0] = 0;
        }
    });
    wer4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            a4[0] = 1;
            a2[0] = 0;
            a3[0] = 0;
            a1[0] = 0;
        }
    });
    if(a1[0] > 0){
        wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer1.setBackgroundColor(Color.parseColor("#42A5F5"));
        wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
    } else  if(a2[0] > 0){

        wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer2.setBackgroundColor(Color.parseColor("#42A5F5"));
        wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }else  if(a3[0] > 0){
        wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer3.setBackgroundColor(Color.parseColor("#42A5F5"));
        wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
    } else if(a4[0] > 0){
        wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
        wer4.setBackgroundColor(Color.parseColor("#42A5F5"));
        wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }
    if(i1 == 1){
        wer1.setImageResource(itemsIcon.getResourceId(i, -1));


    } else if(i1 == 2){
        wer2.setImageResource(itemsIcon.getResourceId(i, -1));

    } else if(i1 == 3){
        wer3.setImageResource(itemsIcon.getResourceId(i, -1));

    } else if(i1 == 4){
        wer4.setImageResource(itemsIcon.getResourceId(i, -1));

    }

    return rootView;
}

ImageView背景不会改变!我在CardViewsRelativeLayoutRelativeLayoutImageView GetOpenFilename

3 个答案:

答案 0 :(得分:0)

请分享xml文件。也许你没有为xml

中的ImageView设置背景

答案 1 :(得分:0)

每个onClick侦听器只是在数组中切换值。我怀疑你真正想要的是在每个处理程序中调用setBackground颜色代码。

从您的代码中,我认为您要做的是:

  1. 无论点击哪个按钮 - 将其背景颜色从白色更改为#42A5F5。

  2. 对于未单击的其他三个按钮,请将背景颜色更改为白色。

  3. 我不知道你班级的名字是什么(我将其称为" YourClass")。但要制作4个ImageViews成员变量。

    class YourClass extends Fragment // I'm guessing this is the declaration, it doesn't matter - use whatever you have now
    {
        ImageView _wer1;
        ImageView _wer2;
        ImageView _wer3;
        ImageView _wer4;
    

    然后在onCreateView中,将这些成员变量中的每一个准确分配给您分配局部变量的位置:

        _wer1 = (ImageView) rootView.findViewById(R.id.imageView);
        _wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
        _wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
        _wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
    

    现在设置每个点击侦听器以调用" ToggleBackgroundColors"。请注意,在每个回调处理程序中传递给此函数的索引值不同。

    _wer1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YourClass.this.ToggleBackgroundColors(0); 
            }
        });
    
    _wer2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YourClass.this.ToggleBackgroundColors(1);
            }
        });
    
    _wer3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YourClass.this.ToggleBackgroundColors(2);
            }
        });
    
    _wer4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YourClass.this.ToggleBackgroundColors(3);
            }
        });
    

    然后实现ToggleBackgroundColors:

    void ToggleBackgroundColors(int which)
    {
        int highlight = Color.parseColor("#42A5F5");
    
        ImageView [] views = {_wer1, _wer2, _wer3, _wer4};
        for (int x = 0; x < views.length; x++)
        {
            int background = (which == x) ? highlight : Color.WHITE;
            views[x].setBackgroundColor(background);
        }
    }
    

答案 2 :(得分:0)

在onClickListener中,您还可以在单​​击它时在imageView上设置colorFilter。

请考虑使用onTouchListener,但是当您触摸按钮而不是单击它时,您希望imageView更改颜色。

ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);