Android 如何在TextView中使用Roboto字体?
如何在我的文本视图中使用roboto字体类型? 我想从xml开始,我的应用程序支持上面的4.1。 下面是我试过的一些东西:Android 如何在TextView中使用Roboto字体?,android,fonts,styles,textview,Android,Fonts,Styles,Textview,如何在我的文本视图中使用roboto字体类型? 我想从xml开始,我的应用程序支持上面的4.1。 下面是我试过的一些东西: <style name="BubbleNumber"> <item name="android:textStyle">normal</item> <item name="android:fontFamily">sans-serif</item> <item name="and
<style name="BubbleNumber">
<item name="android:textStyle">normal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/bubble_text_color</item>
</style>
正常的
无衬线
14便士
@颜色/气泡\文本\颜色
要实现roboto字体类型,您必须在资产文件夹中添加roboto的.ttf文件。并将字体属性设置为文本视图。比如说
TextView title=(TextView)findViewById(R.id.tv);
Typeface font = Typeface.createFromAsset(
activity.getAssets(),
"roboto.ttf");
title .setTypeface(font);
您不能在xml中设置它。Roboto已经是默认的字体类型(从Android 4.0开始) 看 否则,您必须通过编程设置字体 因此,我建议您编写一个类:
public class StyledTextView extends TextView {
public StyledTextView(Context context) {
super(context);
style(context);
}
public StyledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
style(context);
}
public StyledTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
style(context);
}
private void style(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"fonts/roboto.ttf");
setTypeface(tf);
}
}
然后,您可以在普通的XML布局中简单地使用它来替换普通的TextView
<LinearLayout
android:width="match_parent"
android:height="match_parent" >
<com.your.packakge.StyledTextView
android:width="match_parent"
android:height="match_parent" />
</LinearLayout>
我已经这样做了,因为在我的应用程序中,我需要的是所有字体都是Roboto。 如果需要控制edittext或in按钮,请添加一个视图类和 在自定义文本视图中扩展它,或者可以是edittext,然后使用它
public class RobotoTextView extends TextView {
Context context;
public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
public RobotoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public RobotoTextView(Context context) {
super(context);
this.context = context;
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.NORMAL) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoNormal.ttf")/*
* ,
* -
* 1
*/);
} else if (style == Typeface.ITALIC) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoItalic.ttf")/*
* ,
* -
* 1
*/);
} else if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoBold.ttf")/*
* ,
* -
* 1
*/);
}
}
}
现在,在XML布局中,可以这样称呼它
<com.yourpakage.RobotoTextView
android:id="@+id/settings_cover_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="#000000"
android:textSize="16sp"
android:text="Cover Photo"
android:layout_marginLeft="10dp"
/>
还有一个库可以做到这一点。这不是开发者网站上说的。谢谢。。。那很有用!