线性布局背景颜色

4

我正在尝试将整个屏幕的背景更改为白色。我通过将android:background="#ffffff"属性添加到linearLayout来实现它。但是它没有起作用,我不知道我错过了什么?

http://developer.android.com/reference/android/widget/LinearLayout.html

xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="10dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
**android:background="#ffffff"** >

我也尝试了编程方式,类似于这样的代码 setContentView(R.layout.main); LinearLayout linLay = (LinearLayout) findViewById(R.layout.main);
linLay.setBackgroundColor(Color.WHITE);
但应用程序抛出错误。
2个回答

7

您能否提供完整的代码以复现您的问题?

对我来说,这个代码是有效的:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF">

这一行有问题 android:layout_height="wrap_content" - Gainster

0
android:layout_height="fill_parent"

also setBackGroundColor(int) 接受 @color 资源,而不是颜色整数。你需要在 values/colors.xml 中声明它们。


谢谢,但我想知道为什么当我尝试通过代码来执行它时,它会出错? - Gainster
可能是假设了一个不存在的资源 - 但不确定。 - siliconeagle
我看到很多示例使用颜色值,而不是颜色资源,所以我不确定答案的那部分是否正确。文档只是说它是一种“颜色”,实际上使用类似0xFF000000这样的东西确实将颜色设置为黑色——其他值也可以工作。无论如何,希望文档在这方面能够更清晰明了,因为我目前正在追踪一个可能与此有关的错误。 - eselk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接