正如您在附图中看到的那样,Eclipse正在显示资源文件中XML的Tifinagh文本。 但是当我将这个XML解析为listView时它没有显示。我教过它与XML解析有关,所以我基于Hello world教程创建了一个新项目并替换了“Hello world!”用Tifinagh字符(我从XML复制)再次;他们没有表现出来。
我在stackoverflow上检查了similar question,但它还没有答案。
因此;
感谢您的帮助。
我设法通过使用:
在hello world界面中设置字体// Set the tifinagh font
Typeface tf = Typeface.createFromAsset(getAssets(), "t_ircam-webfont.ttf");
TextView tv = (TextView) MainActivity.this.findViewById(R.id.txt);
tv.setTypeface(tf);
但是如何为ListView设置它?
package com.theopentutorials.android.activities;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.theopentutorials.android.beans.PostObj;
import com.theopentutorials.android.xml.XMLPullParserHandler;
public class XMLPullParserActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Prepare the list view to publish item to.
listView = (ListView) findViewById(R.id.list);
List<PostObj> posts = null;
try {
XMLPullParserHandler parser = new XMLPullParserHandler();
posts = parser.parse(getAssets().open("amawal_posts.xml"));
System.out.println("============================== ");
System.out.println("Posts fresh "+ posts);
System.out.println("============================== ");
ArrayAdapter<PostObj> adapter = new ArrayAdapter<PostObj>(this, R.layout.list_item, posts);
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.xmlpull_parser, menu);
return true;
}
}
答案 0 :(得分:1)
经过一些试验和错误,我得到了它的工作:) 由于我从XML获取原始数据到列表视图,我不得不创建一个自定义适配器,检查完整代码:
package com.theopentutorials.android.xml;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter<T> extends BaseAdapter {
private final Typeface mTypeface;
private List<T> objects; // obviously don't use object, use whatever
// you really want
private final Context context;
public MyAdapter(Context context, int resource, List<T> objects) {//Context context, int resource, List<T> objects
this.context = context;
this.objects = (List<T>) objects;
mTypeface = Typeface.createFromAsset(context.getAssets(), "t_ircam-webfont.ttf");
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Object obj = objects.get(position);
TextView tv = new TextView(context);
tv.setTypeface(mTypeface);
tv.setText(obj.toString()); // use whatever method you want for the
// label
// set whatever typeface you want here as well
return tv;
}
}
在我的主要活动中
public class XMLPullParserActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Prepare the list view to publish item to.
listView = (ListView) findViewById(R.id.list);
List<PostObj> posts = null;
try {
// Parse the XML
XMLPullParserHandler parser = new XMLPullParserHandler();
posts = parser.parse(getAssets().open("amawal_posts.xml"));
/*System.out.println("============================== ");
System.out.println("Posts fresh " + posts);
System.out.println("============================== ");*/
listView.setAdapter(new MyAdapter<PostObj>(this, R.layout.list_item, posts));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.xmlpull_parser, menu);
return true;
}
}
在layout / list_item.xml中
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
在layout / main.xml中
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/hello_world"
android:textColor="#CC0033"
android:textSize="16sp" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/text" />
</RelativeLayout>