我正在尝试创建一个显示标题的魔镜应用程序。我成功地抓住了RSS的头条新闻,并将3个顶级标题混合成了字符串。当我将文本添加到选框时,它只显示最后一个标题。
标题字符串示例:
US election: Trump 'encourages Russia to hack Clinton emails' | France church attack: Killers 'pledged allegiance to IS' in video | UN condemns 'barbaric' Boko Haram violence in Nigeria
布局的一部分
<TextView
android:id="@+id/news_headline"
style="@style/MirrorListText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:visibility="gone"
android:textSize="@dimen/mirror_cur_event_text_size"
tools:text=""
tools:visiblity="visible" />
设置textview的功能 编辑添加了整个功能
private NewsModule.NewsListener mNewsListener = new NewsModule.NewsListener() {
@Override
public void onNewNews(String headline) {
if (TextUtils.isEmpty(headline)) {
mNewsHeadline.setVisibility(View.INVISIBLE);
} else {
mNewsHeadline.setVisibility(View.VISIBLE);
Log.d("NEWSMODULE", headline);
mNewsHeadline.setText(headline);
mNewsHeadline.setSelected(true);
}
}
};
编辑:添加了拉RSS的类
public class NewsModule {
public interface NewsListener {
void onNewNews(String headline);
}
public static void getNewsHeadline(final NewsListener newsListener) {
new AsyncTask<Void, Void, String>() {
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
newsListener.onNewNews(s);
}
@Override
protected String doInBackground(Void... params) {
RSSReader rssReader = new RSSReader();
String url = "http://feeds.bbci.co.uk/news/world/rss.xml?edition=us";
try {
RSSFeed feed = rssReader.load(url);
Log.d("NEWSMODULE", "successful load");
String headlines = feed.getItems().get(0).getTitle();// + feed.getItems().get(1).getTitle() + feed.getItems().get(2).getTitle();
Log.d("NEWSMODULE",headlines);
return headlines;
} catch (RSSReaderException e) {
Log.d("NEWSMODULE", "Error parsing RSS");
return null;
} catch (Exception e) {
// not great to catch general exceptions, but this lib is being sketchy
Log.d("NEWSMODULE", "Error parsing RSS");
return null;
}
}
}.execute();
}
}
我知道我已成功将完整的字符串传递给该函数,但唯一的标题出现并且四处移动
"UN condemns 'barbaric' Boko Haram violence in Nigeria"
答案 0 :(得分:0)
TextView的重力不正确。我放android:gravity="left"
之后
作为其中一个属性,内部工作就像一个魅力。