所以我试图将一个已经在assets文件夹中的自定义字体(hello.ttf)设置为ListView。下面是我的java和xml文件。
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>
我尝试了几种添加字体的方法,但似乎无法掌握如何去做。
答案 0 :(得分:2)
您必须在布局文件夹中创建一个布局xml。在该布局xml中,您必须使用自定义字体来自定义textview。
对于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>