我有我從數據庫中檢索到的兩個元素的列表。 我也在使用片段... 我想以自定義的方式對列表視圖中每行的文本進行樣式設置。 的XML文件,其中我有我使用的ListView控件元素TextView的是: useful_numbers_fragment.xml如何爲ListView行文本設置自定義字體?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/useful_nums_h_layout_header">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/useful_nums_group_item"
android:textSize="15dp"
android:layout_gravity="left"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</ListView>
</LinearLayout>
我想設置這樣的TextView的字樣就像這樣:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View
parent = inflater.inflate(R.layout.useful_numbers_fragment, container, false);
Typeface t = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ArialNarrow.ttf");
TextView tv = (TextView) parent.findViewById(R.id.useful_nums_group_item);
tv.setTypeface(t);
return parent;
}
但我得到一個 java.lang.RuntimeException:無法啓動活動 引起:android.view.InflateException:二進制XML文件行#98:錯誤的類文件擴展 引起:java.lang.IllegalStateException:Can not在android.support.v4.app.ListFragment.setEmptyText(ListFragment.java:234) 用自定義內容視圖 用於在... onViewCreated()...
我解決這樣問題:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
ViewGroup parent = (ViewGroup) inflater.inflate(R.layout.useful_numbers_fragment, container, false);
parent.addView(v, 0);
Typeface t = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ArialNarrow.ttf");
TextView tv = (TextView) parent.findViewById(R.id.useful_nums_group_item);
tv.setTypeface(t);
return parent;
}
但是文本字體沒有改變。
SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
Typeface t = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ArialNarrow.ttf");
TextView tv=(TextView)view.findViewById(R.id.useful_nums_group_item);
tv.setText(tv.getText());
tv.setTextColor(Color.RED);
tv.setTypeface(t);
return true;
}
};
simpleCursorAdapter.setViewBinder(binder);
創建'爲列表視圖定製adapter'並設置裏面'getView外部字體()'。 – kId
我正在使用SimpleCursorAdapter ...我用編碼編輯了我的問題...問題是我的行的文本沒有出現... –