更改视图的背景颜色?

时间:2013-01-07 16:14:44

标签: android view background android-linearlayout

我想创建一个linear layout的子类,它的背景颜色必须改为黑色或白色。我写了这段代码:

public class MyLayout extends LinearLayout implements Runnable {

    int color = 0xFFFFFFFF;

    public MyLayout(Context context) {
        super(context);
        Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
                if(color == 0xFF000000){
                    color = 0xFFFFFFFF;
                }else{
                    color = 0xFF000000;
                }
                this.setBackgroundColor(color);
                postInvalidate();
                System.out.println("Color >> " + color);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}

然后我将其设置为我的Activity的ContentView:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyLayout(this));
    }

在模拟器中运行App后,run MyLayout方法毫无例外地执行,但背景颜色总是黑色。我做错了什么?

4 个答案:

答案 0 :(得分:2)

嗨,试试这个吧。

public class MyLayout extends LinearLayout implements Runnable {

    int color = 0xFFFFFFFF;

    public MyLayout(Context context) {
        super(context);
        myView = this;
        Thread t = new Thread(this);
        t.start();
    }

    private View myView;

    @Override
    public void run() {

        try {
            while (true) {
                Thread.sleep(1000);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        if (color == 0xFF000000) {
                            color = 0xFFFFFFFF;
                        } else {
                            color = 0xFF000000;
                        }
                        myView.setBackgroundColor(color);
                        postInvalidate();
                        System.out.println("Color >> " + color);
                    }
                });
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

每当您想通过其他线程对用户界面进行任何更改时,请在runOnUiThread中运行您的代码。

答案 1 :(得分:1)

尝试使用postInvalidate()

的invalidate()

其他提示: 您正在创建新的LinearLayout,而不指定布局大小。 我建议你在使用setContentView:

之前使用setLayoutParams()
MyLayout layout = new MyLayout(this);
layout.setLayoutParams(new LayoutParams(...));
this.setContentView(layout);

此时,您正在创建一个新的布局0大小(高度和宽度)

答案 2 :(得分:0)

你不能在Thread中使用Asynctask

来改变它

https://developer.android.com/reference/android/os/AsyncTask.html

 protected void onPostExecute(Long result) {
         //HERE YOU CAN CHANGE THE COLOR
      }

答案 3 :(得分:0)

在跟进@Hisham Muneer时,我将我的Activity的实例发送到我的班级并且它有效。runOnUiThreadActivity类中的一个方法。这是finall代码:

public class MyLayout extends LinearLayout implements Runnable {

    int color = 0xFFFFFFFF;
    Activity context;

    public MyLayout(Activity context) {
        super(context);
        this.context = context;
        myView = this;
        Thread t = new Thread(this);
        t.start();
    }

    private View myView;

    @Override
    public void run() {

        try {
            while (true) {
                Thread.sleep(1000);

                context.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        if (color == 0xFF000000) {
                            color = 0xFFFFFFFF;
                        } else {
                            color = 0xFF000000;
                        }
                        myView.setBackgroundColor(color);
                        postInvalidate();
                        System.out.println("Color >> " + color);
                    }
                });
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}