如何改变XML背景的Android动态? [英] how to change the xml background in android dynamically?
问题描述
我需要一个逻辑改变给从XML文件中的backgroung颜色的XML编程背景代替。这里我用的setContentView(R.layout.main);以显示在应用xml文件。但我需要改变的main.xml文件的背景色在java中code。
I am need a logic to change the xml background programatically instead of giving the backgroung color from xml file. here i used setContentView(R.layout.main); to show the xml file in application. But i need to change the background color of main.xml file in java code.
推荐答案
以下是说明如何可以将其作为例子。在这里,我已经采取的LinearLayout作为XML文件的根节点。在java中codeI表明,如何设置白色背景。我想,这code可以帮助你。
Following is the example that shows how can you make it. Here i have taken LinearLayout as the root node of xml file. In java code i have shown that how to set white background. I think this code will help you.
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
-----
-----
</LinearLayout>
的Java code
LinearLayout root=(LinearLayout)findViewById(R.id.root);
root.setBackgroundColor(Color.WHITE);
它可以也可以用十六进制code,如下图所示设置颜色,
It can also possible to set color using Hex code as shown below,
root.setBackgroundColor(Color.parseColor("#ffffff"));
甚至可以设置绘制还,
Even you can set drawable also,
root.setBackgroundResource(R.drawable.bg_image);
这篇关于如何改变XML背景的Android动态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!