Android中的自定义字体
我已经读过一些文章,在谷歌上搜索过,但我没能做到 我的问题是关于字体 在Android中,Android中的自定义字体,android,fonts,android-widget,Android,Fonts,Android Widget,我已经读过一些文章,在谷歌上搜索过,但我没能做到 我的问题是关于字体 在Android中,“Android:typeface”中只有4个属性:Normal、Sans、Serif和Monospace 那么,在我的应用程序中使用“Verdana”需要做什么呢 请建议我在Android应用程序中使用此字体的正确方法。这是一个简单的示例。。。在项目的根目录中创建一个名为assets/fonts/的文件夹,然后粘贴TTF字体文件(在本例中为Verdana.TTF)。然后,如果要将该字体应用于,例如文本视图
“Android:typeface”
中只有4个属性:Normal、Sans、Serif和Monospace
那么,在我的应用程序中使用“Verdana”需要做什么呢
请建议我在Android应用程序中使用此字体的正确方法。这是一个简单的示例。。。在项目的根目录中创建一个名为
assets/fonts/
的文件夹,然后粘贴TTF字体文件(在本例中为Verdana.TTF)。然后,如果要将该字体应用于,例如文本视图
,请执行以下操作:
import android.graphics.Typeface;
public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
}
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
这个例子摘自Comonware的书(由MarkMurphy撰写)。您可以。您可以在
导入他们的.jar并在XML中使用它
<com.neopixl.pixlui.components.textview.TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
pixlui:typeface="GearedSlab.ttf" />
您可以使用简单的第三方库为您的文本视图设置各种自定义字体。通过使用此库,您不必担心下载字体并将其添加到assets/fonts文件夹中。还有关于字体对象的创建
此库不提供Verdana字体
但要提供以下字体。您可能希望使用哪一种
- 机器人
- 机器人衬线
- 机器人
- 自由
- 有趣的提倡者
- 安卓国家
- 绿鳄梨
- 认可
简单地说:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
我是这个图书馆的作者。好吧
这个问题已经很老了,但如果有人(2015年)正在寻找如何通过xml代码将自定义字体应用于所有文本视图的答案,请直接参见以下内容:
首先:
我们需要在应用程序目录的资产文件夹中添加自定义字体:
.ttf或.otf都适用于Android
秒:
创建CustomTextView类,该类扩展TextView,如下所示:
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setTypeface(Typeface tf) {
super.setTypeface(FontCache.getFont(getContext(),"fonts/<font_name>"));
}
}
公共类CustomTextView扩展了TextView{
公共CustomTextView(上下文){
超级(上下文);
}
公共CustomTextView(上下文上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
公共CustomTextView(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
公共字体(字体tf){
super.setTypeface(FontCache.getFont(getContext(),“fonts/”);
}
}
第三名:
FontCache类正在CustomTextView的setTypeface()方法中使用。目的是使用HashMap执行基本字体缓存:
public class FontCache {
private static Map<String,Typeface> fontMap = new HashMap<String,Typeface>();
public static Typeface getFont(Context context,String fontname){
if(fontMap.containsKey(fontname)){
return fontMap.get(fontname);
}
else{
Typeface tf = Typeface.createFromAsset(context.getAssets(),fontname);
fontMap.put(fontname,tf);
return tf;
}
}
}
公共类FontCache{
私有静态映射fontMap=newhashmap();
公共静态字体getFont(上下文上下文,字符串fontname){
if(fontMap.containsKey(fontname)){
返回fontMap.get(fontname);
}
否则{
Typeface tf=Typeface.createFromAsset(context.getAssets(),fontname);
fontMap.put(fontname,tf);
返回tf;
}
}
}
第四步:[最后一步]
我们现在所做的就是在需要自定义字体文本视图的地方直接在xml文件中使用CustomTextView:
<<package_name>.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Font Text"
android:textSize ="18sp"
android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/custom_txt"
/>
对不起,如果这已经在某处发布了。只是想分享一下,如果它能帮助别人 要全局更改应用程序的(自定义)字体,请查看
只需将书法添加到gradle.build中,并将以下代码段添加到应用程序中。onCreate()
:
在每个活动中添加以下内容:
import android.graphics.Typeface;
public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
}
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
这就是在应用程序中全局更改字体所需的全部操作。查看文档以了解更多详细信息。是否有方法将此应用于所有文本应用程序范围而不是单个文本视图?是否可以通过xml布局设置控件的字体?是和否。您不能开箱即用。。。但是您可以扩展TextView
来添加这样的功能。@LukeBatley这里发布了一个可能的解决方案@Traxex1909,但是您将如何应用,例如,Verdana字体使用你的解决方案?查看这篇文章:也查看这篇:@ASP它不再需要了,因为谷歌已经为Android O提供了一个选项来设置自定义字体:)查看这篇文章我尝试了你的库,但如果我将TextView改为pixlui TextView,似乎对Android没有效果:textStyle=“bold”。(v 1.0.5)
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}