简体   繁体   中英

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.

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. It seems none of my changes in main.xml are reflected when the app runs.

Here is my main.xml file

<?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

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 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 . Create that file in the same folder that strings.xml is.

Besides that you have some errors:

  • You are missing the setContentView(R.layout.main) on your AleWorldActivity.
  • The id of your ListView is wrong. Change it to: android:id="@android:id/list
  • Add android:layout_height="wrap_content" in your ListView xml.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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