我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!
当前回答
你好,这里我们有一个更好的方法应用在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 8.0 (API 26)引入了与字体相关的新功能。
1)字体可以作为资源使用。
2)可下载的字体。
如果你想在android应用程序中使用外部字体,你可以在apk中包含字体文件或配置可下载的字体。
在APK中包含字体文件:您可以下载字体文件,保存在res/字体文件器中,定义字体族,并在样式中使用字体族。
有关使用自定义字体作为资源的更多详细信息,请参阅http://www.zoftino.com/android-using-custom-fonts
配置可下载字体:通过提供字体提供程序详细信息来定义字体,添加字体提供程序证书并在样式中使用字体。
有关可下载字体的详细信息,请参阅http://www.zoftino.com/downloading-fonts-android
2021年更新:
在res文件夹中创建一个名为font的文件夹并复制您的字体
所有字体名称只能为:小写字母a-z、0-9或下划线。
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/abc_font" />
编程使用:
textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))
对于Android Studio 4.2+,现在甚至有一个菜单选项:
首先创建资产文件夹,然后在其中创建字体文件夹。
然后你可以设置字体从资产或目录如下:
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);
}
}
}
要在运行Android 4.1 (API级别16)及更高版本的设备上使用XML字体功能,请使用支持库26。有关使用支持库的更多信息,请参阅使用支持库一节。
添加字体作为资源,在Android Studio中执行以下步骤:
右键单击res文件夹,进入New > Android资源目录。 出现“新建资源目录”窗口。 在“资源类型”列表中选择字体,然后单击“确定”。 在字体文件夹中添加您的字体文件。
创建字体族 要创建字体族,请在Android Studio中执行以下步骤:
右键单击字体文件夹,选择新建>字体资源文件。出现“新建资源文件”窗口。 输入文件名,然后单击OK。新的字体资源XML在编辑器中打开。 在元素中包含每个字体文件、样式和权重属性。下面的XML演示了如何在字体资源XML中添加与字体相关的属性: 如果你的minSdkVersion是API级别26或更高
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/lobster_italic" />
</font-family>
如果你的minSdkVersion低于API级别26
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="@font/lobster_italic"
app:fontStyle="normal"
app:fontWeight="400" />
</font-family>
然后你可以像这样在任何地方使用它
android:fontFamily="@font/your_custom_font_file"
有很多方法来设置自定义字体家族在字段和我使用如下。
添加字体作为资源,在Android Studio中执行以下步骤:
1)右键单击res文件夹,进入New > Android资源目录。出现“新建资源目录”窗口。
2)在“资源类型”列表中选择“字体”,单击“确定”。
注意:资源目录名必须为字体。
3)在字体文件夹中添加你的字体文件。
在xml文件中添加所需视图中的字体:
注:但你需要做到以下几点:
Android Studio 3.0以上的金丝雀。 你的Activity扩展了AppCompatActivity。 像这样更新你的Gradle文件:
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildtoolsVersion以上26和最低targetSdkVersion要求26
在构建中添加依赖项。gradle文件:
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
推荐文章
- 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中获得当前前景活动上下文?