【发布时间】:2019-02-06 07:00:02
【问题描述】:
我想在整个应用中更改 font-family 或 textview
目前我正在使用以下代码
我在我的res/font 文件夹中添加了font (Click here to check res/font directory)
这是我的font.xml 文件
字体/font_one
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
android:font="@font/oswald_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/oswald_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<font
android:font="@font/oswald_bold"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/oswald_bold"
app:fontStyle="italic"
app:fontWeight="400" />
</font-family>
@font/font_two
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
android:font="@font/opensans_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/opensans_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<font
android:font="@font/opensans_bold"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/opensans_bold"
app:fontStyle="italic"
app:fontWeight="400" />
</font-family>
这是我的 xml 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/sampleTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="?backColor"
android:fontFamily="@font/font_two"
android:padding="20dp"
android:text="@string/app_name"
android:textColor="?textColor"
android:textStyle="normal" />
<TextView
android:id="@+id/sampleTextView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="?backColor"
android:fontFamily="@font/font_one"
android:padding="20dp"
android:text="@string/app_name"
android:textColor="?textColor"
android:textStyle="italic" />
</LinearLayout>
<Spinner
android:id="@+id/fontSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<Spinner
android:id="@+id/themeSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<Button
android:id="@+id/btnChaneFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/test"
android:text="Change Font"
android:textColor="?textColor" />
<Button
android:id="@+id/btnChaneTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/test"
android:text="Change Theme"
android:textColor="?textColor" />
</LinearLayout>
这是我使用
android:fontFamily时的输出,根据我的要求,它工作正常
OUTPUT using android:fontFamily="@font/font_one" from XML
OUTPUT using android:fontFamily="@font/font_two" from XML
这是我的 MainActivity 的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
Spinner fontSpinner, themeSpinner;
Button btnChaneFont, btnChaneTheme;
TextView sampleTextView, sampleTextView2;
int theme = R.style.AppTheme;
String[] fontsArray = new String[]{"Oswald Fonts", "Open Sans Fonts", "Raleway Fonts"};
String[] themeArray = new String[]{"Theme 1", "Theme 2", "Theme 3"};
Typeface typeface = null;
PrefManager prefManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
new ThemeColors(this);
prefManager = new PrefManager(this);
getTheme().applyStyle(prefManager.getTheme(), true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
fontSpinner = findViewById(R.id.fontSpinner);
themeSpinner = findViewById(R.id.themeSpinner);
btnChaneFont = findViewById(R.id.btnChaneFont);
btnChaneTheme = findViewById(R.id.btnChaneTheme);
btnChaneFont.setOnClickListener(this);
btnChaneTheme.setOnClickListener(this);
fontSpinner.setOnItemSelectedListener(this);
themeSpinner.setOnItemSelectedListener(this);
sampleTextView = findViewById(R.id.sampleTextView);
sampleTextView2 = findViewById(R.id.sampleTextView2);
ArrayAdapter<String> fontAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, fontsArray);
fontSpinner.setAdapter(fontAdapter);
ArrayAdapter<String> themeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, themeArray);
themeSpinner.setAdapter(themeAdapter);
}
@Override
public void onClick(View view) {
if (view == btnChaneTheme) {
recreate();
}
if (view == btnChaneFont) {
sampleTextView.setTypeface(typeface);
sampleTextView2.setTypeface(typeface);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
switch (parent.getId()) {
case R.id.fontSpinner:
switch (i) {
case 0:
typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_one);
break;
case 1:
typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_two);
break;
case 2:
typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_three);
break;
}
break;
case R.id.themeSpinner:
switch (i) {
case 0:
prefManager.setTheme(R.style.AppTheme);
break;
case 1:
prefManager.setTheme(R.style.AppTheme2);
break;
case 2:
prefManager.setTheme(R.style.AppTheme3);
break;
}
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
现在,当我使用 java 以编程方式更改文本视图的字体时,我得到了以下结果
OUTPUT using font/font_two" from JAVA
OUTPUT using font/font_two" from JAVA
目前我使用上面的代码面临以下问题
- 当我使用
XML设置font-family时,我会同时获得字体效果 normal 和 italic 但是当我使用 @ 设置font-family987654349@ italic 字体设置不正确(您可以查看两者的结果,我已添加屏幕截图) - 如何使用
java设置font-family - 有没有办法为整个应用程序设置
- 如何使用
java获得与XML相同的font-family行为
任何人都可以帮助我吗?我想更改整个应用程序的font-family
这是我访问过的其他一些帖子,但没有帮助我解决我的问题
- How to change fontFamily of TextView in Android
- Valid values for android:fontFamily and what they map to?
- Android - set TextView TextStyle programmatically?
- Set TextView style (bold or italic)
- https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
- set font at runtime, Textview
- Changing all typefaces of an android application
如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。
【问题讨论】:
标签: java android android-layout fonts textview