Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从xml定义TextView字体?_Java_Android_Fonts_Typeface - Fatal编程技术网

Java 从xml定义TextView字体?

Java 从xml定义TextView字体?,java,android,fonts,typeface,Java,Android,Fonts,Typeface,我有一个使用SimpleAdapter的ListView,每行有两个TextView,我想在两个TextView中的一个使用外部字体。。下面是该类的代码: public class QuranEnglish extends Activity { <...> @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.o

我有一个使用SimpleAdapter的ListView,每行有两个TextView,我想在两个TextView中的一个使用外部字体。。下面是该类的代码:

  public class QuranEnglish extends Activity {
<...>
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quran_english);

    listview=(ListView) findViewById(R.id.QuranEnglishListView);    
    <..add into list..>
    SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.quran_english_row,
            new String[] {"Arabic","English"},
            new int[] {R.id.QuranEnglishTextViewArabic,R.id.QuranEnglishTextViewEnglish});

    listview.setAdapter(adapter);
不是此处某个视图的id。它是示例视图的id,用于填充列表。要更改字体,您需要获得列表的一个视图。如果您选择了其中一个视图,则此操作有效:

TextView view = getSelectedView();
if (view != null) {
  view.setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
}

你有什么错误?NullPointerException?@Vladimir Ivanov是的,NullPointerExceptiongetSelectedView()将获取所选的视图。。我想更改所有文本视图的字体,即使是未选择的。。如何做到这一点?请使用SimpleAdapter的getView()方法。请在此处阅读更多信息:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:orientation="vertical">
    <ListView android:id="@+id/QuranEnglishListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>

</LinearLayout>
((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
R.id.QuranEnglishTextViewArabic 
TextView view = getSelectedView();
if (view != null) {
  view.setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
}