我想在整个应用中更改font-family
中的textview
当前我正在使用以下代码
我已经在font
文件夹(Click here to check res/font directory)内添加了res/font
这是我的font.xml
个文件
font / 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
当前我正在使用上面的代码来面对下面的问题
font-family
设置XML
时,我同时获得了字体效果 normal 和 italic ,但是当我设置{ {1}}使用font-family
的斜体字体设置不正确(您可以检查我是否都添加了屏幕截图的结果)java
设置font-family
java
获得与font-family
相同的XML
行为任何人都可以帮助我更改整个应用程序的java
这是我访问过的其他帖子,但没有帮助我解决问题
如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。
答案 0 :(得分:0)
有一个叫做书法的图书馆 https://github.com/chrisjenx/Calligraphy
或创建自定义TextView并在构造函数中设置字体 这样,您应该将xml中的所有TextView替换为CustomFontTextView
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
public class CustomFontTextView extends AppCompatTextView {
public CustomFontTextView(Context context) {
super(context);
setCustomFont();
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont();
}
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont();
}
private void setCustomFont() {
setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
}
}
Typeface.create()
获取字体不是安全的方法。