English 中文(简体)
我怎样才能在安乐斯打一个字面?
原标题:How can I set a typeface in Android?

我开发了安rio德·RSS阅读器。 我用习俗清单来列出RSS的头衔和形象。 现在我想改变科索沃统计局的头衔。 我如何打上标题案文的字面?

这里是我介绍标题文本概览的改编者类别。

改编人.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;
    }
}
最佳回答

你们可以这样做,

        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;
            }
        }
问题回答

类似情况

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

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

下面一类的类型往往不可行(。 未找到的超常资产

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

并且可能产生这样的错误:

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

因此,你可以采用以下新方式:here

Steps:

1. Make a folder in resources folder in your project like res/font and paste your fonts there

You can alternatively create a font family

Font 家庭是安乐施引入的,用来界定一个老板及其相应的风格。 因此,该系统可以确定用于固定、大胆和斜体式风格和加权组合的资源。 典型的老年家庭就是这样:

<?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>

Note

值得注意的是,我们必须利用海星和航海界定属性。 称号空间确保了特征的后向兼容。

2. Using the font programmatically

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

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

希望:

EditText edittext =(EditText) findViewById(R.id.ed);
String path="F:\MTCORSVA.TTF";
Typeface tf=Typeface.createFromFile(path);
edittext.setTypeface(tf);

您可以这样做:

采用下文所载的习惯案文概览,并在网上浏览。

 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);
        }
    }


   }

页: 1

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

这里使用的是更多的财产,u可以去除不需要的,而getview方法将其定义为:

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

我们还可以使用这种方法:

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

这里是一环。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签