我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!
当前回答
首先在字体文件夹中添加font.ttf文件。然后在onCreate方法中添加这一行
Typeface typeface = ResourcesCompat.getFont(getApplicationContext(), R.font.myfont);
mytextView.setTypeface(typeface);
这是我的xml
<TextView
android:id="@+id/idtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:gravity="center"
android:text="My Text"
android:textColor="#000"
android:textSize="10sp"
/>
其他回答
在Project -> app(或您的app名称)-> src -> main ->右键单击->新建->目录中创建文件夹资产。 然后在资产中创建一个名为fonts的新目录。
将字体分配给textView:
TextView textView = (TextView) findViewById(R.id.your_textView);
final Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/your_font_name");
Your_font_name包含字体扩展名。
现在有很多应用字体的方法,最简单的方法之一是这样的, 1)右键单击res文件夹 进入“新建> Android资源目录”。
2)从资源类型列表中选择字体,然后单击确定。
3)把你的字体文件放在字体文件夹里。
你好,这里我们有一个更好的方法应用在android上的EditTexts和TextViews字体,并将其应用于整个项目。
首先,你需要使字体文件夹。以下是步骤。
1:进入(项目文件夹)然后应用>src>main
2:在主文件夹中创建名为assets/fonts的文件夹。
3:把你的字体放进字体文件夹。这里是' mavenpro - regular。ttf'
下面是在EditText上应用自定义字体的步骤,使用这种方法可以在每个输入上应用字体。
1:创建一个类MyEditText(你喜欢的名字…)
2:扩展EditText
3:使用你的字体
下面是代码示例;
public class MyEditText extends EditText {
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyEditText(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf");
setTypeface(tf);
}
}
}
这里是如何使用它的代码。
MyEditText editText = (MyEditText) findViewById(R.id.editText);
editText.setText("Hello");
或者在你的xml文件中
<MyEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#fff"
android:textSize="16dp"
android:id="@+id/editText"
/>
在项目中添加字体
添加字体作为资源,在Android Studio中执行以下步骤:
1 -右键单击res文件夹,进入New > Android资源目录。 出现“新建资源目录”窗口。
2 -在“资源类型”列表中,选择“字体”,单击“确定”。 3 -添加您的字体文件在字体文件夹只是一个简单的复制和粘贴。注意字体的名称应该是小写的。
使用XML布局中的字体
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>
向样式添加字体
<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/lobster</item>
</style>
以编程方式使用字体
科特林:
val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface
JAVA:
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
芬兰湾的科特林回答
如果你需要在代码方面使用字体,你可以使用这个功能,它也有版本代码控制。
fun getFontJarvisWhite(): Typeface {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) resources.getFont(R.font.jarvis_white)
else context?.let { ResourcesCompat.getFont(it, R.font.jarvis_white) }!!
}
推荐文章
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件
- 修改抽射超时时间
- 如何通过数据从第二个活动到第一个活动时按回?——安卓
- 如何在android中获得当前前景活动上下文?