Android的ListView的样式和颜色? [英] android ListView Style and colors?
问题描述
我有问题,从selector.xml项目的风格样式列表视图我(背景)
i have problem styling my listview from selector.xml items style ( background )
颜色不切换其显示的所有项目的黑色,我想改用物品的颜色黑和白之间,现在的目标是从列表视图适配器交换物品的颜色,但是当我从列表视图适配器切换项目颜色时,我点击任何项目项目背景变成蓝色!
the colors not switched its show all the items black , i want to switch items colors between black and white , for now aim switching items colors from listview Adapter, but when i switch items colors from listview Adapter when i click on any item the item background become blue !
这是我的code
public class Home_ForumsList_listview extends ArrayAdapter<Object>{
private int[] colors = new int[] { 0x30ffffff, 0x30808080 };
Context context;
private LayoutInflater mInflater;
@SuppressWarnings("rawtypes")
ArrayList ob;
int resource ;
/*================================================
* Setup ListView
*===============================================*/
@SuppressWarnings("unchecked")
public Home_ForumsList_listview (Context context, int resource, @SuppressWarnings("rawtypes") ArrayList objects) {
super(context, resource,objects);
this.context = context;
this.ob = objects;
this.resource = resource;
mInflater = LayoutInflater.from(context);
}
/*================================================
* Items Counter
*===============================================*/
public int getCount() {
return ob.size();
}
/*================================================
* Item Posistion JSON
*===============================================*/
public JSONObject getItem(JSONObject position) {
return position;
}
/*================================================
* Item Position
*===============================================*/
public long getItemId(int position) {
return position;
}
/*================================================
* Hold Views inside Chant Bit View
*===============================================*/
static class ViewHolder {
TextView forum_title;
TextView forum_desc;
TextView forum_catagories;
TextView forumcode;
Button popup_but_id;
RatingBar ratingsmall;
}
/*================================================
* Setup Each View raw by raw
*===============================================*/
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
JSONObject r = (JSONObject) getItem(position);
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.a_home_forumslist_bit, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.forum_title = (TextView) convertView.findViewById(R.id.flist_forum_title);
holder.forum_desc = (TextView) convertView.findViewById(R.id.flist_forum_desc);
holder.forum_catagories = (TextView) convertView.findViewById(R.id.flist_forum_catagories);
holder.forumcode = (TextView) convertView.findViewById(R.id.flist_forumcode);
holder.ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
int colorPos = position % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
try {
holder.forum_title.setText(r.getString("forum_title"));
if ( r.getString("forum_desc").equals(""))
{
holder.forum_desc.setVisibility(View.GONE);
}
float z = (float) r.getInt("rate");
holder.ratingsmall.setRating(z);
holder.forum_desc.setText(r.getString("forum_desc"));
holder.forumcode.setText( context.getString(R.string.forumcode) + " : ( "+ r.getLong("forumcode") + " ) ");
holder.forum_catagories.setText( " : " + r.getString("forum_catagories"));
} catch (JSONException e) {
e.printStackTrace();
}
return convertView;
}
}
我GOOGLE了它如何风格列表视图,显示颜色相同的所有例子来自适配器开关,有没有办法从selector.xml风格列表视图项目?
i googled it how to style listview , all the examples showing same colors switch from Adapter , is there any way to style listview items from selector.xml ?
对不起我的英语不好^ _ ^我希望你得到它
sorry for my bad English ^_^ i hope you got it
推荐答案
这听起来像你想交替背景色在列表中,这样的项目0是白色的,第1项是黑色的,第2项是白等。
It sounds like you want to alternate background colors in your list, so item 0 is white, item 1 is black, item 2 is white, etc.
首先,定义颜色的Android资源:
First, define your colors as Android resources:
RES / colors.xml:
res/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="black">#000000</color>
</resources>
然后,使用这些资源标识符为背景。现在,你给它的颜色的十六进制为int,这是错误的 - 它需要int这是一个资源标识符。像这样的:
Then, use these resource identifiers as the backgrounds. Right now, you're giving it the color hex as an int, which is wrong -- it expects an int that is a resource identifier. Like this:
private int[] colors = new int[] { R.color.white, R.color.black };
这应该工作。试试吧。
要摆脱蓝色的亮点,你可以做选择,并使用这些,而不是仅仅黑白:
To get rid of the blue highlight, you could make selectors and use those instead of just black and white:
绘制/ dark_selector.xml
drawable/dark_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="@color/gray"
android:pressed="true" />
<item android:color="@color/black" />
</selector>
然后使用可绘制作为背景。
Then use the drawables as your backgrounds.
private int[] colors = new int[] {
R.drawable.lightSelector,
R.drawable.darkSelector
};
这篇关于Android的ListView的样式和颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!