简体   繁体   English

我在 main.xml 中设置的背景颜色在应用程序运行时未显示

[英]The background color I'm setting in main.xml isn't showing when the app runs

I'm setting a background color in main.xml.我在 main.xml 中设置背景颜色。

When I preview the layout in Eclipse, the background color shows up correctly, but when the app runs on my device, the background color is default black.当我在 Eclipse 中预览布局时,背景颜色正确显示,但当应用程序在我的设备上运行时,背景颜色默认为黑色。 It seems none of my changes in main.xml are reflected when the app runs.应用程序运行时,我对 main.xml 的更改似乎都没有反映出来。

Here is my main.xml file这是我的 main.xml 文件

<?xml version="1.0" encoding="utf-8"?>

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lst"
android:layout_width="match_parent"
android:background="@color/listViewBG"
android:divider="@drawable/divider"
/>

Here is the OnCreate in the main activity这是主要活动中的 OnCreate

public class AleWorldActivity extends ListActivity
{
String classes[] = { "Movies", "Pictures" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(AleWorldActivity.this, android.R.layout.simple_list_item_1, classes));
}

Any ideas?有任何想法吗? Thanks谢谢

Here is my strings.xml这是我的字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, AleWorldActivity!</string>
<string name="app_name">Ale World</string>
<color name="listViewBG">#e101f5</color>

</resources>

Kevin凯文

You should declare your color on colors.xml .您应该在colors.xml上声明您的颜色。 Create that file in the same folder that strings.xml is.在与strings.xml相同的文件夹中创建该文件。

Besides that you have some errors:除此之外,您还有一些错误:

  • You are missing the setContentView(R.layout.main) on your AleWorldActivity.您缺少 AleWorldActivity 上的setContentView(R.layout.main)
  • The id of your ListView is wrong.你的 ListView 的 id 是错误的。 Change it to: android:id="@android:id/list将其更改为: android:id="@android:id/list
  • Add android:layout_height="wrap_content" in your ListView xml.在您的 ListView xml 中添加android:layout_height="wrap_content"

Change @color/listViewBG to #FFFFFF see if the screen changes colors that means that there is a problem with you declaring it in your strings.xml.@color/listViewBG更改为#FFFFFF看看屏幕是否更改 colors 这意味着您在 strings.xml 中声明它时出现问题。

http://developer.android.com/resources/samples/SoftKeyboard/res/values/colors.html http://developer.android.com/resources/samples/SoftKeyboard/res/values/colors.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM