【问题标题】:How to set customize different font style in Android如何在Android中设置自定义不同的字体样式
【发布时间】:2016-02-15 18:26:51
【问题描述】:

我想为我的“按钮”文本和“编辑文本框”文本设置不同的字体样式,例如 Times New Roman、Calibri、Cambria 和 Georgia。如何设置不同的字体,例如我想将登录按钮文本更改为 Calibri 字体。我不知道如何从 MS Office 或字体文件中设置或导入字体。请推荐我,谢谢..

我的 XML 代码在这里

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="   User Name  " />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="   Password  " 
        android:fontFamily="Tekton Pro Ext"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="Tekton Pro Ext"
        android:text="   Login  " />
</LinearLayout>

布局

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    您需要在项目的 assets 文件夹下创建字体文件夹,并将您的 TTF 放入其中,在 main 或您的活动中,您可以将字体设置为

    TextView myTextView=(TextView)findViewById(R.id.textBox);
    Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
    myTextView.setTypeface(typeFace);
    

    【讨论】:

      【解决方案2】:

      也许最好看看这篇准确回答您正在寻找的内容的帖子:

      1-How to change fontFamily of TextView in Android

      2-How to change the font on the TextView?

      简而言之,您必须执行以下操作: 将字体放在 /fonts 目录下的 assets 文件夹中,然后使用这行代码:

      Button bt = (Button) findViewById(R.id.myButton);
      Typeface face = Typeface.createFromAsset(getAssets(),
                  "fonts/epimodem.ttf");
      bt.setTypeface(face);
      

      【讨论】:

        【解决方案3】:

        以上所有答案都是正确的。如果您想在整个应用程序中使用具有不同字体的 ButtonTextView,请执行以下操作。

        public class FontsArePriceyTextView extends TextView {
        
            public MyTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                setCostlyFont();
            }
        
            public MyTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
                setCostlyFont();
            }
        
            public MyTextView(Context context) {
                super(context);
                setCostlyFont();
            }
        
            private void setCostlyFont() {
                if (!isInEditMode()) {
                    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "filenameofyourfont.ttf");
                    setTypeface(tf);
                }
            }
        }
        

        然后在您的布局中,将TextView 替换为yourpackagename.FontsArePriceyTextView。例如。

        <com.company.projectname.widgets.FontsArePriceyTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Your awesome stylised text" />
        

        你应该很高兴。

        【讨论】:

        • 我如何使用这个构造函数类的所有文本字段可以访问我的项目的所有文本视图?
        • 只需将 XML 布局中的 TextView 标记替换为您自定义的 Text View 类名称即可。您可以将其用作普通文本视图,例如。 TextView tv = (TextView) findViewById(R.id.stylisedTextView)。我想您现在可以为 Buttons 或您自己想要的任何东西实现相同的功能。
        • 任何可能为新字体添加直接 xml textview 的方法
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-17
        • 2013-12-15
        • 2017-01-20
        • 2016-06-17
        • 2014-07-01
        • 1970-01-01
        相关资源
        最近更新 更多