2

正如您在所附图片中看到的那样,Eclipse 正在资源文件中正确显示来自 XML 的Tifinagh文本。但是当我将此 XML 解析为 listView 时,它不会显示。我教它与 XML 解析有关,所以我基于 Hello world 教程创建了一个新项目并替换为“Hello world!” 使用 Tifinagh 字符(我从 XML 中复制的)并再次使用;他们不显示。

我在stackoverflow上检查了一个类似的问题,但还没有答案。

所以;

  1. Android 是否支持 Tifinagh?
  2. 如果没有,是否有任何解决方法可以解决此问题?

谢谢你的帮助。

Tifinagh_not_displayed_on_android_emulator

更新

我设法通过使用以下方法在 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;
    }

}
4

1 回答 1

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;
    }
}

在布局/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>

在布局/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>
于 2013-09-27T16:47:59.383 回答