2016-12-12 5 views
2

したがって、既にassetsViewフォルダにあるカスタムフォント(hello.ttf)をListViewに設定しようとしています。以下は私のjavaとxmlファイルです。ListViewのカスタムフォント

faListFragment.java

public class faListFragment extends Fragment { 


    public faListFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_fa_list, container, false); 

     String[] faList = { "One", 
          "Two", 
          }; 

     ListView listView = (ListView) view.findViewById(R.id.listFa); 


     ArrayAdapter<String> listviewAdapter = new ArrayAdapter<String>(
       getActivity(), 
       android.R.layout.simple_list_item_1, 
       faList 
     ); 




     listView.setAdapter(listviewAdapter); 

     // Inflate the layout for this fragment 
     return view; 
    } 

} 

、これが私のfragment_fa_list.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.me.hello.faListFragment"> 

    <!-- TODO: Update blank fragment layout --> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/listFa" /> 
</FrameLayout> 

である私は、フォントを追加するいくつかの方法を試してみましたが、カントはかなりいるようですそれについてどうやって行くのか把握する。

答えて

2

レイアウトフォルダに1つのレイアウトxmlを作成する必要があります。そのレイアウトxmlでは、あなたはカスタムフォントを使ってカスタムテキストビューを利用する必要があります。 CustomTextViewリンクについては

Using custom font in android TextView using xml

ArrayAdapter<String> listviewAdapter = new ArrayAdapter<String>(
       getActivity(), 
       R.layout.row_item, R.id.text , 
       faList 
     ); 
listView.setAdapter(listviewAdapter); 

編集: マイCustomTextViewクラス

public class MyTextView extends TextView { 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     if (!isInEditMode()) { 
      Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"); 
      setTypeface(tf); 
     } 
    } 

} 

row_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="15dp" 
    android:background="#00000000" 
    android:orientation="vertical"> 




    <pakagename.MyTextView 
     android:id="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/title" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="5dp" 

     android:text="CEO" 
     android:textColor="#000" 
     android:textSize="16dp" /> 


</RelativeLayout> 
+0

私はこれにかなり従っていませんでした。 –

+0

TextViewのCustomFontについては、これを行う必要があります。新しいものを見つけたら、私にもお勧めしてください。 –

+0

はあなたがそれよりも少し詳細であれば納得するでしょう。私はJavaとAndroidの開発には非常に新しいです。 –

関連する問題