我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!
当前回答
使用支持库26.0(和Android O)字体可以轻松地从资源加载:
Typeface typeface = ResourcesCompat.getFont(Context context, int fontResourceId)
方法的文档。
更多信息可以在这里找到。
其他回答
首先创建资产文件夹,然后在其中创建字体文件夹。
然后你可以设置字体从资产或目录如下:
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/HandmadeTypewriter.ttf");
tv.setTypeface(face);
File font = new File(Environment.getExternalStorageDirectory(), "MgOpenCosmeticaBold.ttf");
if (font.exists()) {
tv = (TextView) findViewById(R.id.file);
face = Typeface.createFromFile(font);
tv.setTypeface(face);
} else {
findViewById(R.id.filerow).setVisibility(View.GONE);
}
}
}
选择“File>New>Folder>Assets Folder” 单击finish 右键单击资产并创建一个名为fonts的文件夹 把你的字体文件在资产>字体 使用下面的代码改变你的textView的字体 TextView TextView = (TextView) findViewById(R.id.textView); 字体字体=字体字体。createfromasset (getAssets(), "fonts/yourfont.ttf"); textView.setTypeface(字体);
您可以使用简单的EasyFonts第三方库来设置各种自定义字体到您的TextView。通过使用这个库,你应该不必担心下载和添加字体到assets/fonts文件夹。还有关于字体对象的创建。您将从创建资产文件夹也自由。
简单:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
这个库提供了许多类型的字体。
首先在字体文件夹中添加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"
/>
我想补充一下我对Android- o和Android Studio 2.4的回答
在res文件夹下创建名为font的文件夹。下载要添加到项目示例谷歌fonts中的各种字体 在xml用户字体家族中 例子: < TextView android: fontFamily = " @font / indie_flower” android: layout_width = " wrap_content " android: layout_height = " wrap_content " android:填充= " 10 dp” android:文本= " @string / sample_text " / >
3.如果您希望它以编程的方式使用下面的代码
Typeface typeface = getResources().getFont(R.font.indie_flower);
textView.setTypeface(typeface);
要了解更多信息,请链接到我的博客文章字体样式的Android与Android Studio 2.4
推荐文章
- 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中获得当前前景活动上下文?