我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但是不幸的是,我无法找到放置“assets”文件夹的位置!
我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但是不幸的是,我无法找到放置“assets”文件夹的位置!
更新 2023:
在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+,现在甚至有一个菜单选项:
在字段上设置自定义字体族有许多方法,我使用了以下方式。
要将字体添加为资源,请在Android Studio中执行以下步骤:
1)右键单击res文件夹,然后转到New > Android resource directory。将出现New Resource Directory窗口。
2)在资源类型列表中选择font,然后单击OK。
注意:资源目录的名称必须为font。
在xml文件中的所需视图中添加字体:
注意:但是,您需要以下内容:
Android Studio版本应为3.0 canary以上。
您的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。
在 build.gradle 文件中添加依赖项:
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
我认为我们可以使用Google字体,而不是下载.ttf文件。这很容易实现。只需按照以下步骤操作即可。
第一步:打开您项目中的layout.xml,并在属性中选择文本视图的字体系列(请参考屏幕截图)
第二步:如果您的字体不在其中,请在字体系列中选择“更多字体”选项。然后会打开一个新窗口,在那里,您可以输入所需的字体并从列表中选择所需的字体,例如:Regular,Bold,Italic 等,如下图所示。
第三步:然后您会发现/res文件夹中会自动生成一个包含所选字体xml文件的字体文件夹。
然后,您可以直接在xml中使用此字体系列:
android:fontFamily="@font/josefin_sans_bold"
或者你可以通过编程的方式来实现这一点,使用
Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
fontText.setTypeface(typeface);
大家好,我们有一种更好的方法来同时应用于Android上EditText和TextView上的字体,并将其应用于整个项目。
首先,您需要创建一个“fonts”文件夹。以下是步骤。
1:进入(项目文件夹)然后app>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-O和Android Studio 2.4添加我的答案。
在res文件夹下创建名为font的文件夹。下载你想要添加到项目中的各种字体,例如Google字体
在你的xml文件中使用自定义字体族
例如:
<TextView
android:fontFamily="@font/indie_flower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/sample_text" />
3. 如果您想以编程方式实现,请使用以下代码
Typeface typeface = getResources().getFont(R.font.indie_flower);
textView.setTypeface(typeface);
了解更多信息,请访问我的博客文章链接:使用Android Studio 2.4为Android添加字体样式
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
TextView
设置各种自定义字体。使用此库,您无需担心下载并将字体添加到assets / fonts文件夹中,也无需创建Typeface对象或asset文件夹。TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
这个库提供了许多种字体类型。
将字体分配给 textView:
TextView textView = (TextView) findViewById(R.id.your_textView);
final Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/your_font_name");
你的字体名称包含字体扩展。