【问题标题】:How can I set a typeface in Android?如何在 Android 中设置字体?
【发布时间】:2012-05-26 14:42:57
【问题描述】:

我开发了一个 Andriod RSS 阅读器应用程序。我使用自定义列表视图来列出 RSS 标题及其图像。现在我想更改 RSS 标题的字体。如何为我的标题文本视图设置字体?

这是我在其中设置标题 Textview 的适配器类。

Adapter.java

public class InternationalAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public InternationalAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)


            vi = inflater.inflate(R.layout.list_row, null);




        TextView title = (TextView)vi.findViewById(R.id.title); // For this Textview I want to set Typeface.
        TextView date = (TextView)vi.findViewById(R.id.artist);

        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);

        HashMap<String, String> news = new HashMap<String, String>();
        news = data.get(position);

        // Setting all values in listview
        title.setText(International.Title[position]);
        date.setText(International.Date[position]);
        imageLoader.DisplayImage(International.image[position], thumb_image);
        return vi;
    }
}

【问题讨论】:

  • 这个问题在 SO 上被问了 100 次。你为什么不参考那些?。
  • 我知道如何在 Activity 上执行此操作。但这是不同的。
  • 你不能在一个活动上这样做。您只能在视图上执行此操作。

标签: android textview


【解决方案1】:

你可以用这个,

        public class InternationalAdapter extends BaseAdapter {

            private Activity activity;
            private ArrayList<HashMap<String, String>> data;
            private static LayoutInflater inflater=null;
            public ImageLoader imageLoader; 

            public InternationalAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
                activity = a;
                data=d;
                inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                imageLoader=new ImageLoader(activity.getApplicationContext());
            }

            public int getCount() {
                return data.size();
            }

            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                View vi=convertView;
                if(convertView==null)


                    vi = inflater.inflate(R.layout.list_row, null);




                TextView title = (TextView)vi.findViewById(R.id.title); // For this Textview I want to set Typeface.
                TextView date = (TextView)vi.findViewById(R.id.artist);
//Added Here
        Typeface font = Typeface.createFromAsset(
        activity.getAssets(), 
        "fonts/androidnation.ttf");
    title .setTypeface(font);

                ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);

                HashMap<String, String> news = new HashMap<String, String>();
                news = data.get(position);

                // Setting all values in listview
                title.setText(International.Title[position]);
                date.setText(International.Date[position]);
                imageLoader.DisplayImage(International.image[position], thumb_image);
                return vi;
            }
        }

【讨论】:

  • 我使用了这个方法,但是在eclipse中,出现错误。“方法getContext()未定义为InternationalAdpater类型”..
  • 而不是 getContext() 尝试使用活动
  • 最欢迎...:) 快乐编码..:)
【解决方案2】:

这样试试

Typeface font = Typeface.createFromAsset(
    getContext().getAssets(), 
    "fonts/androidnation.ttf");
title .setTypeface(font);

【讨论】:

    【解决方案3】:

    来源:https://segunfamisa.com/posts/custom-fonts-with-android-support-library

    像下面这样设置字体通常不起作用 (Android Runtime Exception font asset not found)

    Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/montserrat_regular.ttf");
    

    并可能导致如下错误:

    java.lang.RuntimeException: Font asset not found fonts/montserrat_regular.ttf
    

    因此,您可以使用给定here的新方法

    步骤:

    1。在项目的资源文件夹中创建一个文件夹,如 res/font 并将您的字体粘贴到那里

    您也可以创建字体系列

    Font family 是 Android 中引入的东西,用于定义一组字体及其对应的样式。因此,系统可以确定常规、粗体和斜体样式和粗细配置使用什么字体资源。典型的字体系列如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <!-- regular -->
    <font
        android:font="@font/josefinslab_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
    
        app:font="@font/josefinslab_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />
    
    <!-- italic -->
    <font
        android:font="@font/josefinslab_italic"
        android:fontStyle="italic"
        android:fontWeight="400"
    
        app:font="@font/josefinslab_italic"
        app:fontStyle="italic"
        app:fontWeight="400" />
    
    </font-family>
    

    注意

    需要注意的一个非常重要的事情是,我们必须同时使用 android 和 app 命名空间来定义属性。应用命名空间确保该功能向后兼容。

    2。以编程方式使用字体

    Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
    myTextView.setTypeface(typeface);
    

    来源:https://segunfamisa.com/posts/custom-fonts-with-android-support-library

    希望这会有所帮助:)

    【讨论】:

      【解决方案4】:
      EditText edittext =(EditText) findViewById(R.id.ed);
      String path="F:\\MTCORSVA.TTF";
      Typeface tf=Typeface.createFromFile(path);
      edittext.setTypeface(tf);
      

      你可以用这个:)

      【讨论】:

        【解决方案5】:

        使用下面给出的自定义文本视图并在那里设置字体

         public class MyTextView extends TextView{
        
         public MyTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                init();
                rotate();
            }
        
            public MyTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
                rotate();
            }
        
            public MyTextView(Context context) {
                super(context);
                init();
                rotate();
            }
        
            private void rotate() {
                // TODO Auto-generated method stub
                setSelected(true);
            }
        
            private void init() {
                if (!isInEditMode()) {
                    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/TrajanPro-Regular.otf");
                    setTypeface(tf);
                }
            }
        
        
           }
        

        并且在您的列表视图行中,xml 意味着在 list_row 布局中添加 textview 作为

           <YourpackageName.MyTextView
            android:layout_marginTop="10dip" android:id="@+id/title"
            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:textSize="22px"
                android:textColor="#34A4c5"
                android:ellipsize="marquee"
                android:maxWidth="220dp" 
                android:fadingEdge="horizontal"
                android:marqueeRepeatLimit="marquee_forever"
                android:scrollHorizontally="true"  
                android:singleLine="true"
                android:layout_marginLeft="10dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_marginRight="10dp"></YourpackageName.MyTextView>
        

        这里使用了更多属性,您可以删除不需要的属性,并在您的 getview 方法中将其定义为

              MyTextView title = (MyTextView)vi.findViewById(R.id.title); 
        

        【讨论】:

          【解决方案6】:

          您可以使用此库更改任何视图上的字体,只需将字体文件名与绑定的 xml 代码一起传递。 https://github.com/ChathuraHettiarachchi/TypeFaced

          1.文本视图

          2.编辑文本

          3.切换

          4.切换

          5.按钮

          6.单选按钮

          7.复选框

          <com.chootdev.typefaced.TypeFacedTextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Hello World!"
                  app:textView_font="YOUR_FONT_NAME.ttf/>
          

          【讨论】:

            【解决方案7】:

            我们也可以用这个方法:

            t.setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
            

            这里的等宽字体是一种字体。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-01-24
              • 1970-01-01
              • 2011-09-10
              • 2013-12-15
              • 2018-02-10
              • 2017-01-20
              • 2016-06-17
              相关资源
              最近更新 更多