【问题标题】:Linear Layout background color线性布局背景颜色
【发布时间】:2011-03-27 01:25:25
【问题描述】:

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

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"** >

我也尝试过编程,类似这样 设置内容视图(R.layout.main); 线性布局 linLay = (LinearLayout) findViewById(R.layout.main);
linLay.setBackgroundColor(Color.WHITE);

但应用程序抛出错误

【问题讨论】:

    标签: android android-widget android-layout


    【解决方案1】:

    您能否发布复制您的问题所需的完整代码?

    这对我来说很好用:

    <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"
    【解决方案2】:
    android:layout_height="fill_parent"
    

    setBackGroundColor(int) 也采用 @color 资源,而不是颜色整数。你在 values/colors.xml 中声明它们

    【讨论】:

    • 谢谢,但我想知道为什么当我通过代码尝试它时为什么它会爆炸?
    • 可能是假设的资源不存在 - 但不确定。
    • 我看到很多使用颜色值而不是颜色资源的示例,所以我不确定部分答案是否正确。文档只是说它也是一种“颜色”,实际上使用 0xFF000000 之类的东西实际上确实将颜色设置为黑色——其他值也可以。无论如何,希望文档清楚这一点,因为我目前正在寻找一个我认为可能与此相关的错误。
    猜你喜欢
    • 2018-09-15
    • 1970-01-01
    • 2023-03-12
    • 2015-02-14
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多