2014-07-17 45 views
-1

我有我從數據庫中檢索到的兩個元素的列表。 我也在使用片段... 我想以自定義的方式對列表視圖中每行的文本進行樣式設置。 的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); 
+0

創建'爲列表視圖定製adapter'並設置裏面'getView外部字體()'。 – kId

+0

我正在使用SimpleCursorAdapter ...我用編碼編輯了我的問題...問題是我的行的文本沒有出現... –

回答

0

創造出在CustomAdapter設置外部字樣延伸SimpleCursorAdapter自定義適配器類。

做一些變化Fragment's onCreateView(...)

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 
     View v= (View) inflater.inflate(R.layout.useful_numbers_fragment, null); 
     Typeface t = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ArialNarrow.ttf"); 
     TextView tv = (TextView) v.findViewById(R.id.useful_nums_group_item); 
     tv.setTypeface(t); 
     return v; 
    } 

代碼片斷

public class CustomAdapter extends SimpleCursorAdapter { 

      private Context mContext; 
      private Context appContext; 
      private int layout; 
      private Cursor cr; 
      private final LayoutInflater inflater; 
      private Typeface t; 

      public CustomAdapter(Context context,int layout, Cursor c,String[] from,int[] to) { 
       super(context,layout,c,from,to); 
       this.layout=layout; 
       this.mContext = context; 
       this.inflater=LayoutInflater.from(context); 
       this.cr=c; 
       t = Typeface.createFromAsset(context.getAssets(), "fonts/ArialNarrow.ttf"); 
      } 

      @Override 
      public View newView (Context context, Cursor cursor, ViewGroup parent) { 
        return inflater.inflate(layout, null); 
      } 

      @Override 
      public void bindView(View view, Context context, Cursor cursor) { 
       super.bindView(view, context, cursor); 
       TextView titleS=(TextView)view.findViewById(R.id.TitleSong); 
       TextView artistS=(TextView)view.findViewById(R.id.Artist); 
       titleS.setTypeface(t); 
       artistS.setTypeface(t); 
       int Title_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 
       int Artist_index=cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST); 

       titleS.setText(cursor.getString(Title_index)); 
       artistS.setText(cursor.getString(Artist_index)); 

      } 

    } 
相關問題