安卓应用程序背景颜色无法设置。

5

我最近开始开发一个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”中的文本是灰色的,而上面的TextView中的“Fact:”文本是白色的,我尝试将其颜色更改为红色,但它不起作用。它仍然是白色的。
有什么建议吗?我仍然没有成功地解决这个问题;谢谢。

1
如果您想使用@color/background_grey,我认为您需要创建一个color.xml文件。否则,您可以使用Android默认值进行检查...请参见此问题https://dev59.com/sG865IYBdhLWcg3whfBg - O'Mutt
将您的布局颜色更改为 @android:color/white 并从那里开始。另外,您在哪里使用了 view_fact?您的主要布局中只有 LinearLayout 吗? - Tiago Almeida
我现在打算尝试将布局颜色修改为那个。是的,view_fact 是另一个页面,当点击按钮时,它会将内容视图切换到该页面。 - EM-Creations
我刚刚尝试在main.xml中设置android:background="@android:color/white",但仍然没有改变背景颜色。 - EM-Creations
线性布局里面有什么?有可能有什么东西阻挡了线性布局的背景吗? - O'Mutt
<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="@android:color/white" >这是整个线性布局的起始标签,背景仍然是黑色。 - EM-Creations
4个回答

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

3
我经常使用这样的颜色参考,而不需要一个特定的color.xml文件。当按照strings.xml中所示的定义时,它们可以正常工作。 - Robert Nekic
1
不,那不是问题所在。我尝试将我的颜色定义移动到color.xml中,但仍然无法解决问题。不过还是谢谢你的建议。 - EM-Creations
1
不对,我错了。请看我的答案,同时http://developer.android.com/guide/topics/resources/more-resources.html 告诉我们文件名是任意的。 - Paul D'Ambra

0

请原谅我问一些显而易见的问题:您在Activity onCreate中调用了setContentView(R.layout.main)吗?布局中是否有其他内容填充了LinearLayout并覆盖了灰色背景?


在我的onCreate方法中,我正在调用:setContentView(R.layout.main); - EM-Creations
你在用Eclipse吗?如果你打开布局文件,应该能够切换到图形化布局选项卡,并且看到背景颜色已经正确设置。如果你在那里看到了正确的颜色,那么可能是其他问题导致的。 - Robert Nekic

-1

看了一下你在评论中链接的pastie,我认为你需要删除开头的<?xml version="1.0" encoding="utf-8"?>这一行。Eclipse不会在布局中使用该xml根标记构建新创建的应用程序。

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

如果你删除那一行,我很想知道会发生什么...


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

通过在/res/values/strings.xml中添加<string name="background_grey">#E8E8E8</string>,可以更改背景。
如果我在布局中声明它为android:background="@color/background_grey",并在strings.xml中使用<color name="background_grey">#E8E8E8</color>也可以实现相同的效果。
如果我设置res/values/colors.xml <= 我已经使用了colors.xml(而你使用了color.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"
>

然后背景也会改变...

如果它对你来说表现不同,那么可能出了些问题,或者你使用的布局与上面简单的示例不同,那么在该布局中可能出了些问题。


是的,那仍然有效。我也尝试了编程设置,有什么建议吗? - EM-Creations
你能添加完整的布局文件吗? - Paul D'Ambra

-1

好的,我尝试了很多不同的方法来设置背景颜色,但都没有成功。现在我还遇到了文本不更新的问题。所以我打算重新制作它。我将使用Eclipse,并且如果我仍然遇到问题,我会发布一个新的问题。感谢您的建议。


更新,我在 Eclipse 中没有遇到任何问题。 - EM-Creations

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