嘿,我想将其中一个TextView的TypeFace更改为外部字体..我该怎么办?
代码:
public class English 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);
每行的xml(quran_english_row.xml):
<?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">
<LinearLayout android:gravity="right" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="fill_parent">
<TextView android:textSize="21px" android:text="TextView" android:id="@+id/QuranEnglishTextViewArabic" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<TextView android:textSize="21px" android:text="TextView" android:id="@+id/QuranEnglishTextViewEnglish" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
布局的xml(quran_english.xml):
<?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>
由于NullPointer而崩溃:
((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
那么如何才能到达行中的TextView来更改其TypeFace?感谢
答案 0 :(得分:1)
您可以尝试按
获取每个列表项的布局查看rowView = adapter.getView(...);
然后通过
获取布局的TextViewTextView txt =(TextView)rowView .findViewById(R.id.QuranEnglishTextViewEnglish); txt.setTypeFace(...);
并设置您的TypeFaces。尚未测试,但我想它可以工作。
但我建议您扩展自己的BaseAdapter。然后创建另一个列表行类,为您的BaseAdapter提供膨胀的布局。在那里,您将能够轻松地为每个TextView设置字体。
答案 1 :(得分:0)
试试这个链接它有效http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html看看上面显示的步骤和类MyTextView和MyButton布局