gpt4 book ai didi

android - Android应用程序 ListView 中的多种文本颜色

转载 作者:行者123 更新时间:2023-11-29 00:45:55 25 4
gpt4 key购买 nike

好的伙计们,这是交易。我的应用程序中有一个 RSS 阅读器,它解析 XML 并将其吐到 Listview 中......现在我将颜色设置为绿色,但我需要标题为绿色以及我在每个中放入的描述部分行为黑色。

这是我的显示提要部分的代码:

public class CampusNews extends Activity implements OnItemClickListener
{
int checker = RSSHandler.checker;
public final String RSSFEEDOFCHOICE = "myurl";

private static final int SELECT = 0;
private static final int REFRESH = 1;

public final String tag = "RSSReader";
private RSSFeed feed = null;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.campus_news_layout);

// go get our feed!
feed = getFeed(RSSFEEDOFCHOICE);

// display UI
UpdateDisplay();

}


private RSSFeed getFeed(String urlToRssFeed)
{
try
{
// setup the url
URL url = new URL(urlToRssFeed);

// create the factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// create a parser
SAXParser parser = factory.newSAXParser();

// create the reader (scanner)
XMLReader xmlreader = parser.getXMLReader();
// instantiate our handler
RSSHandler theRssHandler = new RSSHandler();
// assign our handler
xmlreader.setContentHandler(theRssHandler);
// get our data via the url class
InputSource is = new InputSource(url.openStream());
// perform the synchronous parse
xmlreader.parse(is);
// get the results - should be a fully populated RSSFeed instance, or null on error
return theRssHandler.getFeed();
}
catch (Exception ee)
{
// if we have a problem, simply return null
return null;
}
}



private void UpdateDisplay()
{
TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
ListView itemlist = (ListView) findViewById(R.id.itemlist);


if (feed == null)
{
feedtitle.setText("No RSS Feed Available");
return;
}

// feedtitle.setText(feed.getTitle());
feedpubdate.setText(feed.getPubDate());




ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems()){

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View for custom Background color of feeds
View view = super.getView(position, convertView, parent);

if (position % 2 == 0){
view.setBackgroundColor(0xffe4e4e4); // gray
((TextView) view).setTextColor(0xff00DD00); // text color
((TextView) view).setTextSize(15.0f);


} else {
view.setBackgroundColor(0xffffffff); // White
((TextView) view).setTextColor(0xff00DD00); // text color
((TextView) view).setTextSize(15.0f);
}
return view;
}


};




itemlist.setAdapter(adapter);

itemlist.setOnItemClickListener(this);

itemlist.setSelection(0);

}


@SuppressWarnings("rawtypes")
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");

Intent itemintent = new Intent(this,ShowDescription.class);

Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description", feed.getItem(position).getDescription());
b.putString("link", feed.getItem(position).getLink());
b.putString("pubdate", feed.getItem(position).getPubDate());

itemintent.putExtra("android.intent.extra.INTENT", b);

startActivity(itemintent);
}

}

这是我的代码,告诉它如何将标题和描述部分放在每一行中。

public class RSSItem 
{
private String _title = null;
private String _description = null;
private String _link = null;
private String _category = null;
private String _pubdate = null;


RSSItem()
{
}
void setTitle(String title)
{
_title = title;
}
void setDescription(String description)
{
_description = description;
}
void setLink(String link)
{
_link = link;
}
void setCategory(String category)
{
_category = category;
}
void setPubDate(String pubdate)
{
_pubdate = pubdate;
}
String getTitle()
{
return _title;
}
String getDescription()
{
return _description;
}
String getLink()
{
return _link;
}
String getCategory()
{
return _category;
}
String getPubDate()
{
return _pubdate;
}
public String toString()
{
// limit how much text we display
if (_title.length() > 42)
{

return _title.substring(0, 42) + "..." + "\n" + _description.substring(3, 110) + "...";
}
return _title + "\n" + _description.substring(3, 110) + "...";

}
}

我四处搜索了很多,这就是我解决大部分问题的方法,但我找不到一种方法可以将多种文本颜色放在每个项目的一行中。提前致谢!

最佳答案

你会想看看这个:http://developer.android.com/resources/faq/commontasks.html#selectingtext .它涵盖了文本的样式部分。

编辑:您可以使用 ForegroundColorSpan 的实例更改文本中点点滴滴的颜色。

编辑#2:尝试这样的事情:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View for custom Background color of feeds
View view = super.getView(position, convertView, parent);

if (position % 2 == 0){
view.setBackgroundColor(0xffe4e4e4); // gray
((TextView) view).setTextColor(0xff00DD00); // text color
((TextView) view).setTextSize(15.0f);


} else {
view.setBackgroundColor(0xffffffff); // White
((TextView) view).setTextColor(0xff00DD00); // text color
((TextView) view).setTextSize(15.0f);
}
RSSItem item = (RSSItem)getItem(position);
String title = item.getTitle(), base = item.toString();
SpannableString str = new SpannableString(base);
if(title.length() > 42){
str.setSpan(new ForegroundColorSpan(0xFF00FF00), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(0xFF000000), title.length()+1, base.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else{
str.setSpan(new ForegroundColorSpan(0xFF00FF00), 0, 45, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(0xFF000000), 46, base.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
((TextView) view).setText(str);
return view;
}

关于android - Android应用程序 ListView 中的多种文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6365458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com