我花了一整天的时间寻找答案,找到了很多,但都不是我需要的。因此,我有一个非常简单的ListView菜单和一个TextView项目文件。我想为菜单中的项目设置Segoe打印字体,我知道,我需要适配器,但我做不到。救命!!!
menu.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/menu_top"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/menu_top">
</LinearLayout>
<ListView
android:id="@+id/ListView_Menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fon_android"
android:divider="@drawable/menu_line"
android:textStyle="bold" >
</ListView>
</LinearLayout>
menu_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_prop"
android:layout_width="fill_parent"
android:textSize="19px"
android:text="test string"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:shadowRadius="5"
android:textColor="@color/menu_color"
android:shadowDy="3"
android:shadowDx="3" />
Main_Menu.java的一部分:
public class Main_menu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
String[] items = {
getResources().getString(R.string.system),
getResources().getString(R.string.convert),
getResources().getString(R.string.forks),
getResources().getString(R.string.margin)};
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.menu_item, items);
menuList.setAdapter(adapt);
}
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
}
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.menu_prop);
Typeface font = Typeface.createFromAsset(getAssets(), "segoe_print.ttf");
textView.setTypeface(font);
return textView;
}
private AssetManager getAssets() {
// TODO Auto-generated method stub
return null;
}
}
字体不变。。。
view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
.getContext().getAssets(), "fonts/DroidSerif.ttf"));
这里是自定义适配器的代码:
private class CostumiseAdapter extends ArrayAdapter<item>{
public CostumiseAdapter(ArrayList<item> items) {
super(getActivity(), 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.scroll_fragment, null);
}
Item item = getItem(position);
TextView text = (TextView) convertView.findViewById(R.id.text_view);
text.setText(item.mString);
RadioButton b1 = (RadioButton) convertView.findViewById(R.id.radio_b1);
b1.setChecked(item.mB1);
RadioButton b2 = (RadioButton) convertView.findViewById(R.id.radio_b2);
b2.setChecked(item.mB2);
CheckBox cB = (CheckBox) convertView.findViewById(R.id.check_box);
cB.setChecked(item.mB2);
return convertView;
}
}
我从http://androide-examples.blogspot.com/2013/11/android-scroll-list.html有关完整示例,请参阅。
在这个例子中有一些语义错误。所以你必须将一些项目固定为项目(大写)。。。
您需要创建一个自定义适配器来设置自定义字体。这是最小代码:
public class CustomAdapter extends ArrayAdapter<String> {
//Contructor here
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.myTextView);
// Here you set the typeface as you do for the TextView
textView.setTypeFace(....);
}
}
这应该为您的Listview提供自定义字体。
我找到了一个设计!首先,我为Adapter创建了另一个类:
public class Typefaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String name) {
synchronized (cache) {
if (!cache.containsKey(name)) {
String path = "fonts/"+name;
try {
Typeface t = Typeface.createFromAsset(c.getAssets(), path);
cache.put(name, t);
} catch (Exception e) {
e.printStackTrace();
}
}
return cache.get(name);
}
}
}
然后在主适配器中,我添加了两个字符串:
Typeface tf = Typefaces.get(getContext(), "tahoma.ttf");
holder.txtTitle.setTypeface(tf);
仅此而已!它在工作!))感谢大家的帮助和回答。
在xml:中尝试
android:fontFamily="somefont.ttf"
从android 4.1+可以使用以下字体:http://robotofont.com/
android:fontFamily="sans-serif"
android:fontFamily="sans-serif-thin"
android:fontFamily="sans-serif-light"
android:fontFamily="sans-serif-condensed"
您也可以通过程序设置字体:
TextView t = (TextView) findViewById(R.id.name);
Typeface font = Typeface.createFromAsset(getAssets(),"font/somefont.ttf");
t.setTypeface(font);
将字体文件放在文件夹中。。。