【问题标题】:Android App Background Colour Not SettingAndroid 应用背景颜色未设置
【发布时间】:2012-09-24 19:54:49
【问题描述】:

我最近开始开发一款 Android 应用。我不是编程菜鸟,自 2009 年以来我一直在使用 Java 编程,但这是我的第一个 Android 应用程序。我已经尝试遵循一些关于如何设置背景颜色的教程,但它根本不适合我。我无法弄清楚我做错了什么。

在我的布局目录中,我有名为 main.xml 和 view_fact.xml 的文件。他们都使用线性布局,我的 LinearLayout 标签如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_grey"
>

在我的“values”目录中,“strings.xml”的内容是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Facts</string>
<color name="background_grey">#E8E8E8</color>
</resources>

但是背景颜色并没有从默认的黑色改变。

我也尝试在我的 AndroidManifest.xml 文件中添加:“android:theme="@android:style/Theme.Light"”。

这些都不行,有什么建议吗?

谢谢。

在我的 view_fact.xml 页面上,它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_grey"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fact:"
android:padding="10dp"
android:textColor="#080808"
/>

<TextView 
android:id="@+id/factData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text=""
/>
<Button
android:id="@+id/anotherFactButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Another Fact"/>
</LinearLayout>

有趣的是,“factData”中的文本是灰色的,而上面的文本视图中的文本:“Fact:”是白色的,我尝试将其颜色更改为红色,但它不起作用。它保持白色。

有什么建议吗?我仍然没有设法让这个工作;谢谢。

【问题讨论】:

  • 如果你想使用@color/background_grey,我相信你需要制作一个color.xml文件。否则你可以使用 android 默认值来检查...看到这个问题stackoverflow.com/questions/3769762/…
  • 将布局颜色更改为 @android:color/white 并从那里开始。另外,你在哪里使用你的 view_fact?你的 main 中只有那个 LinearLayout 吗?
  • 我现在尝试将布局颜色更改为该颜色。是的,view_fact 是另一个页面,当单击按钮时,它会将内容视图更改为该页面。
  • 我刚刚尝试在 main.xml 中设置:android:background="@android:color/white" 但它仍然没有改变背景颜色。
  • 线性布局里面有什么?是不是有什么东西挡住了线性布局背景?

标签: java android


【解决方案1】:

如果你想使用@color/color_name,你必须创建一个 Color.xml 文件,然后它就会以你尝试使用它的方式访问。

【讨论】:

  • 我一直使用这样的颜色参考,没有特定的 color.xml 文件。如 strings.xml 所示定义时,它们可以正常工作。
  • 不,这不是问题所在。我尝试将我的颜色定义移动到 color.xml 中,但它仍然不起作用。不过感谢您的建议。
  • 不,我错了。见我的回答,但也 developer.android.com/guide/topics/resources/… 告诉我们文件名是任意的
【解决方案2】:

请原谅有些明显的问题,但是:您是否在 Activity onCreate 中调用 setContentView(R.layout.main) ?布局中是否还有其他东西填充了 LinearLayout 并覆盖了背景灰色?

【讨论】:

  • 在我调用的 onCreate 方法中:setContentView(R.layout.main);
  • 您在使用 Eclipse 吗?如果您打开布局文件,您应该能够切换到图形布局选项卡并看到背景颜色设置正确。如果您在那里看到正确的颜色,那么可能是发生了其他事情。
【解决方案3】:

查看pastie you linked in the comment 我认为您需要删除开头的&lt;?xml version="1.0" encoding="utf-8"?&gt; 行。 Eclipse 不会在布局中使用该 xml 根目录构建新创建的应用程序..

虽然这很奇怪,因为我在 Android 开发者网站 (for example) 上找到的前两个示例包含它

我很想知道如果您删除该行会发生什么...


如果我在 Eclipse 中使用

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

然后背景颜色发生变化。

如果我把它改成

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@string/background_grey"
>

通过将&lt;string name="background_grey"&gt;#E8E8E8&lt;/string&gt; 添加到/res/values/strings.xml,背景会发生变化。

如果我在布局中将其声明为android:background="@color/background_grey" 并在strings.xml 中声明为&lt;color name="background_grey"&gt;#E8E8E8&lt;/color&gt;,这也有效

如果我设置 res/values/colors.xml http://developer.android.com/guide/topics/resources/more-resources.html 告诉我们文件名是任意的,正如你在声明中看到的那样在 strings.xml 文件中工作

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background_grey">#E8E8E8</color>
</resources>

并将布局声明为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_grey"
>

然后背景也会改变...

如果它的行为对您而言不一样,则说明发生了一些奇怪的事情,或者您使用的布局与上面的简单示例不同,那么,该布局中发生了一些奇怪的事情。

【讨论】:

  • 是的,这仍然有效。我也试过以编程方式设置它,有什么建议吗?
  • 您可以添加完整的布局文件吗?
【解决方案4】:

好的,我尝试了很多不同的方法来尝试设置背景颜色,但都没有奏效。而且我现在遇到文本不更新的问题。所以我打算尝试重新制作它。如果我仍然遇到问题,使用 eclipse 和 ill 会提出一个新问题。感谢您的建议。

【讨论】:

  • 更新,我在eclipse中没有这个问题。
猜你喜欢
  • 2013-08-04
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 2012-07-31
  • 1970-01-01
  • 2016-06-18
相关资源
最近更新 更多