如何更改Android中的文本/字体设置TextView?例如,如何使文本变粗?
我需要为我的整个应用程序使用某些字体.我有相同的.ttf文件.是否可以在应用程序启动时将其设置为默认字体,然后在应用程序的其他位置使用它?设置后,如何在布局XML中使用它?
我想为我正在创建的Android应用程序使用自定义字体.
我可以从Code中单独更改每个对象的字体,但我有数百个.
所以,
是否可以在Android应用程序中设置自定义字体?
我尝试了这里发布的内容,但我不知道我的extends Application课程在哪里......
有帮助吗?
编辑:
我尝试了以下方法:
添加一个扩展自的新类 Application
从我的电话中调用这个新课程AndroidManifest.xml.
我按照我的风格添加了它.
MyApp.java:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
FontsOverride.setDefaultFont(this, "DEFAULT", "raleway_regular.ttf");
// This FontsOverride comes from the example I posted above
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".MyApp"
android:theme="@style/AppTheme">
....
Run Code Online (Sandbox Code Playgroud)
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:fontFamily">default</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但我的字体仍然没有变换......任何想法?
然后MyApp调用该类.但对我的字体没有影响......
EDIT2:我意识到在为按钮设置自定义样式后,我的按钮会应用自定义字体.这是我的自定义按钮样式:
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
以下是它现在的样子:
所以:我的按钮正在应用样式,但不是TextView.有关为什么我的自定义字体没有应用于应用程序中的所有项目的任何想法?
我开发了一个android项目.在这个项目中,文本字体默认为android:sans.
现在我想将整个项目的默认文本字体替换为roboto字体.
我怎样才能做到这一点?
有没有办法在Android的主题中添加自定义字体?
我已阅读快速提示:自定义Android字体,但在这里我们必须以编程方式将自定义字体添加到文本中.
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
txt.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)
但我想按样式/主题设置自定义字体.
如何使用我的xml资产文件夹中添加的自定义字体?我知道我们可以setTypeface()在java中使用方法,但我们必须在我们使用它的地方执行此操作TextView.那么还有更好的方法吗?
我想应用一些字体说Times New Roman只适用于我的应用程序. 不是对整个系统而是对特定的观点.据我所知
要将字体应用于特定视图,我们将字体文件存储在资产文件夹中,并按如下方式进入应用程序.
1] Typeface mFace = Typeface.createFromAsset(getContext().getAssets(),
"fonts/samplefont.ttf");
textView.setTypeface(mFace);
2] To apply font to whole application I can replace the DroidSans.ttf file with my font file.
Run Code Online (Sandbox Code Playgroud)
我可以使用第一种方式将字体应用到我的应用程序,但它不是一个好的解决方案,因为我需要在任何地方进行修改,如果有更好的方法,我不想这样做.
好的,我知道如何更改我的应用程序中单个文本框上使用的字体,但我希望我的应用程序中的所有文本都使用此自定义字体和颜色,目前我能想到的唯一方法是参考每个框并将它们设置为正确的字体和颜色.虽然它可行但似乎是一种笨重的方法,有更好的方法吗?
我想使用自定义字体文件.以下是我的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/custom_font"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the Chantelli Antiqua font." />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
txt.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)
它的工作.但我想在整个项目中使用这种自定义字体.
所以我必须放在一个所有Class都可以使用它的地方.不需要为每个人写TextView.
我在哪里以及如何使用此自定义字体.
我创建了一个自定义文本视图,只是为了解决上一个问题中的问题
我经历了很多教程和许多样本.我创建了一个自定义文本视图,但无法将其添加到任何布局文件中.它只是在布局文件上抛出一个错误,指出找不到该类.但是我写了同一个班级.
我想知道的是,在创建自定义文本视图时,我需要注意什么?我唯一的要求是创建一个自定义文本视图,以便在我使用的所有文本视图中包含arial字体.并且还可以为按钮视图扩展此功能.
谢谢