简体   繁体   English

以编程方式更改布局的背景颜色

[英]Change the background color of the layout programmatically

Android Studio 0.4.6
minSdkVersion 10
targetSdkVersion 19

I have an activity called ReadingLamp and a Relativelayout called activity_reading_lamp.xml . 我有一个名为ReadingLamp的活动和一个名为activity_reading_lamp.xml的Relativelayout。

I am programmatically trying to set the layout to a different background color. 我正在以编程方式尝试将布局设置为不同的背景颜色。

In my onCreate and set the content view to this layout. 在我的onCreate中,将内容视图设置为此布局。

setContentView(R.layout.activity_reading_lamp);

I try and get the root view by doing the following: 我尝试通过执行以下操作获取根视图:

mActivityBackground = getWindow().getDecorView().getRootView();

Then later in my app I want to change the color so I do like this: 然后在我的应用程序中我想改变颜色,所以我喜欢这样:

mActivityBackground.setBackgroundColor(Color.parseColor("#0cf5ff"));

However, the above line doesn't do anything to change the background. 但是,上面的行没有做任何改变背景的事情。

I have also tried doing the following: 我也试过做以下事情:

mActivityBackground = (RelativeLayout)findViewById(R.layout.activity_reading_lamp);

Where am I going wrong in my code? 我的代码在哪里出错?

you have declared, setContentView(R.layout.activity_reading_lamp); 你已声明, setContentView(R.layout.activity_reading_lamp); in your Activity . 在你的Activity Then you should look for the view you want to change the background color. 然后,您应该查找要更改背景颜色的视图。 It has to belongs to R.layout.activity_reading_lamp . 它必须属于R.layout.activity_reading_lamp

View view = findViewById(R.id.declared_inside_reading_lamp);

Then you can call 然后你可以打电话

view.setBackgroundColor(Color.GREEN)

您必须确保activity_reading_lamp.xml中的所有布局都具有透明背景

You need to specify which background you need to set. 您需要指定需要设置的背景。 For example, you can make id for the parent layout on your activity then do: 例如,您可以为活动的父布局设置id,然后执行以下操作:

RelativeLayout parentLayout = (RelativeLayout)findViewById(R.id."your parent layout id and not your activity name");
parentLayout.setBackgroundColor(Color.TRANSPARENT);

If you would like to make a transition to the new color try this: 如果您想转换到新颜色,请尝试以下操作:

@SuppressLint("NewApi") private void tintColor(View rootView, String newColor) {
    // currentColor can be given as a new parameter or set as a field
    ColorDrawable[] color = {
            new ColorDrawable(Color.parseColor(currentColor)),
            new ColorDrawable(Color.parseColor(newColor)) };
    TransitionDrawable trans = new TransitionDrawable(color);
    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        rootView.setBackgroundDrawable(trans);
    } else {
        rootView.setBackground(trans);
    }
    trans.startTransition(ANIMATION_TIME); // ANIMATION_TIME : time in milliseconds
}

The code accepts hex color strings. 代码接受十六进制颜色字符串。

RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM