对于ListView控件,Android的设置字体 [英] Set font for ListView, Android

查看:167
本文介绍了对于ListView控件,Android的设置字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在寻找答案,找到了很多,但所有这些都没有什么,我需要。
所以,我有一个的ListView pretty简单的菜单以及与TextView的项目文件。我想设置的Segoe-打印字体在我的菜单项,我知道,我需要适配器,但我不能这样做。帮助!

menu.xml文件:
    

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
       机器人:方向=垂直
       机器人:layout_width =FILL_PARENT
       机器人:layout_height =FILL_PARENT>    <的LinearLayout机器人:ID =@ + ID / menu_top
       机器人:方向=垂直
       机器人:layout_width =FILL_PARENT
       机器人:layout_height =WRAP_CONTENT
       机器人:背景=@绘制/ menu_top>
       < / LinearLayout中>< ListView控件
          机器人:ID =@ + ID / ListView_Menu
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
          机器人:背景=@绘制/ fon_android
          机器人:分=@绘制/ menu_line
          机器人:文字样式=大胆>
< /&的ListView GT;
< / LinearLayout中>

menu_item.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<的TextView
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / menu_prop
机器人:layout_width =FILL_PARENT
机器人:TEXTSIZE =19px
机器人:文字=测试字符串
机器人:layout_gravity =左
机器人:layout_height =WRAP_CONTENT
机器人:shadowRadius =5
机器人:文字颜色=@彩色/ menu_color
机器人:shadowDy =3
机器人:shadowDx =3/>

Main_Menu.java的一部分:

 公共类MAIN_MENU延伸活动{
    @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.menu);
        ListView的菜单列表=(ListView控件)findViewById(R.id.ListView_Menu);
        的String []项目= {
                getResources()。的getString(R.string.system)
                getResources()。的getString(R.string.convert)
                getResources()。的getString(R.string.forks)
                。getResources()的getString(R.string.margin)};
        ArrayAdapter<串GT;适应=新ArrayAdapter<串GT;(这一点,R.layout.menu_item,项目);
    menuList.setAdapter(适应);
}

CustomAdapter.java:

 公共类CustomAdapter扩展ArrayAdapter<串GT; {       公共CustomAdapter(上下文的背景下,INT资源的String []对象){
    超级(上下文,资源,对象);
    // TODO自动生成构造函数存根
}    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
              查看查看= super.getView(位置,convertView,父母);
              TextView中的TextView =(TextView中)view.findViewById(R.id.menu_prop);
              字体的字体= Typeface.createFromAsset(getAssets(),segoe_print.ttf);
              textView.setTypeface(字体);
            返回的TextView;
       }    私人AssetManager getAssets(){
        // TODO自动生成方法存根
        返回null;
    }
  }

字体并不会改变...


解决方案

  view.txtViewTitle.setTypeface(Typeface.createFromAsset(父
                .getContext()getAssets(),字体/ DroidSerif.ttf))。

I spent all day searching for an answer, found a lot, but all of them are not what I need. So, I have a pretty simple menu with ListView and a file for items with TextView. I want to set Segoe-Print font for items in my menu, I know, that I need adapter, but I can't do it. Help!!!

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" />

A part of 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;
    }
  }

The font doesn't change...

解决方案

view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
                .getContext().getAssets(), "fonts/DroidSerif.ttf"));

这篇关于对于ListView控件,Android的设置字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆