【问题标题】:Custom font in listview Android?listview Android中的自定义字体?
【发布时间】:2015-08-13 03:23:22
【问题描述】:

如何添加自定义字体? 这是我的代码:

public void run() {
    ListAdapter adapter = new SimpleAdapter(DiaListActivity.this, diaList, R.layout.list_item, new String[]{TAG_SRNO, TAG_NAME}, new int[]{R.id.srno, R.id.name});
    setListAdapter(adapter);
}

我们将不胜感激。

【问题讨论】:

标签: android listview fonts


【解决方案1】:
    In R.layout.list_item  layout file if you have any textview then you can set this below code::-

    <com.example.TextViewPlus
            android:id="@+id/textViewPlus1"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="@string/showingOffTheNewTypeface"
            foo:customFont="saxmono.ttf">
        </com.example.TextViewPlus>


    Put these class file into your package: 

    TextViewPlus.java

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.widget.TextView;

    public class TextViewPlus extends TextView {
        private static final String TAG = "TextView";

        public TextViewPlus(Context context) {
            super(context);
        }

        public TextViewPlus(Context context, AttributeSet attrs) {
            super(context, attrs);
            setCustomFont(context, attrs);
        }

        public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setCustomFont(context, attrs);
        }

        private void setCustomFont(Context ctx, AttributeSet attrs) {
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
            String customFont = a.getString(R.styleable.TextViewPlus_customFont);
            setCustomFont(ctx, customFont);
            a.recycle();
        }

        public boolean setCustomFont(Context ctx, String asset) {
            Typeface tf = null;
            try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
            } catch (Exception e) {
                Log.e(TAG, "Could not get typeface: "+e.getMessage());
                return false;
            }

            setTypeface(tf);  
            return true;
        }

    }



attrs.xml: (in res/values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

【讨论】:

    【解决方案2】:

    我通常做的是,创建一个自定义 TextView 并将其设置为我的内容的视图,

    public class CustomTextView extends TextView {
    
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomTextView(Context context) {
            super(context);
        }
    
        public void setTypeface(Typeface tf, int style) {
            if (style == Typeface.BOLD) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
            } else {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf"));
            }
        }
    }
    

    现在只需像这样用自定义的 TextView 替换常规的 TextView,

    <TextView
     android:id="@+id/R.id.srno"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     />
    

    进入,

     <com.example.CustomTextView
      android:id="@+id/R.id.srno"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      />
    

    这将用自定义的 TextView 更改普通的 TextView,从而添加字体。

    注意:确保您已经创建了一个名为“fonts”的文件夹并将字体文件放在那里。

    如果也可以通过添加一行来自定义和添加不同的字体,

    例如如果你使用,

    android:textStyle="bold"
    

    它将设置字体类型 Roboto-Regular.ttf。这是在 CustomTextView 类中定义的,

    if (style == Typeface.BOLD) {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
            }
    

    【讨论】:

      【解决方案3】:

      知道了……

      @Override
      public View getView(int pos, View convertView, ViewGroup parent){
          View v = convertView;
          if(v == null) {
              LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              v = vi.inflate(R.layout.list_item, null);
          }
      
          HashMap<String,String> value = diaList.get(pos);
      
          TextView tv = (TextView)v.findViewById(R.id.name);
          Typeface custom_fontG = Typeface.createFromAsset(getAssets(), "fonts/oriya.ttf");
          tv.setTypeface(custom_fontG);
          tv.setText(value.get(TAG_NAME));
      
          TextView tv2 = (TextView)v.findViewById(R.id.srno);
          Typeface custom_fontH = Typeface.createFromAsset(getAssets(), "fonts/oriya.ttf");
          tv2.setTypeface(custom_fontH);
          tv2.setText(value.get(TAG_SRNO));
      
          return v;
      }
      };
      
      // updating listview
      
      setListAdapter(adapter);
      

      【讨论】:

        猜你喜欢
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-03
        • 2013-12-08
        • 2011-04-12
        • 2015-12-29
        • 1970-01-01
        相关资源
        最近更新 更多