开发者

How to change the color of list item using List Activity file

I have seen some links which am not able to trace it out.I am trying to change the color of the list item using java code in my activity class. Iam not using any Code for list view in my XML file. So, please Can any one tell me how to change the color of the list item from default white to black and here is my sample code .

ListView listView;
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "India", "Malaysia" };
// Use your own layout and point the adapter to the UI elements which
// contains the label
TextView tv = new TextView(getApplicationContext());
        tv.setText("Select Country");

        listView = getListView();
        listView.addHeaderView(tv);
       listView.setCacheColorHint(Color.rgb(36, 33, 32));
        listView.setBackgroundColor(Color.rgb(225, 243, 253));

        this.setListAdapter(new ArrayAdapter<String&开发者_JAVA技巧gt;(this,android.R.layout.simple_expandable_list_item_1,names));

}

Thanks in advance


I don't think that changing the code in core Android components is a good idea. If you want to try something along the lines suggested by krishna5688, a better idea would probably be to better to create your own simple_expandable_list_item_1.xml (I would suggest giving it a more relevant name). The SDK version of the layout file can be found in <your SDK folder>\platforms\android-<whichever version you're using>\data\res\simple_expandable_list_item_1.xml. Put the contents into an xml file in your own res/layout folder, and make the changes there. Then change the line of code

    this.setListAdapter(new ArrayAdapter<String>(this,R.layout.<whatever you named your list item>,names));

Note that it's no longer android.R.layout...


If this is a change that you want to apply to the whole application (i.e. you generally want white backgrounds and black text for your UI widgets), then you're probably best changing the theme of the whole application in your manifest file to a 'Light' theme. Try adding this attribute to the application node in your manifest file:

android:theme="@android:style/Theme.Light"

Hope that helps!


You are using android.R.layout.simple_expandable_list_item_1.xml file as a ListView item. in xml code of android.R.layout.simple_expandable_list_item_1.xml you can set background tag in main Layout with some color as shown bellow.

android:background="#00FF44"

You can use any hexadecimal color code there.

I hope it will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜