在 Android 中更改 EditText 的字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10766943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change font for EditText in Android?
提问by William L.
Is there any way to change the font for a EditText in Android? I want it to match the font's I set for all my textViews.
有什么方法可以更改 Android 中 EditText 的字体?我希望它与我为所有 textView 设置的字体相匹配。
回答by Samir Mangroliya
editText.setTypeface(Typeface.SERIF);
As like as for TextView.
就像 TextView 一样。
<TextView
...
android:typeface="serif"
... />
Edit: Above is the XML
编辑:上面是 XML
回答by Shankar Agarwal
Solution1:: Just call these method by passing parent view as argument.
解决方案 1:: 只需通过将父视图作为参数传递来调用这些方法。
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof EditText) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
}
} catch (Exception e) {
}
}
Solution2:: you can subclass the TextView class with your custom font and use it instead of textview.
解决方案 2:: 您可以使用自定义字体对 TextView 类进行子类化,并使用它代替 textview。
public class MyEditView extends EditText{
public MyEditView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyEditView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyEditView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
setTypeface(tf);
}
}
}
回答by Sara
Create a fontsfolder on assetsfolder and put your .ttf font file, then into onCreate() function write:
在assets文件夹上创建一个fonts文件夹并放入 .ttf 字体文件,然后在 onCreate() 函数中写入:
EditText editText =(EditText)findViewById(R.id.insert_yors_edit_text_layout);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yours_font.ttf");
editText.setTypeface(type);
回答by sirvan alie
You can create fontfolder under res directory in last version Android studio 3 .Then copy and past it you'r font to fontfolder. Now just add fontFamily
to EditTexttag xml.
您可以在最新版本的 Android studio 3 中的 res 目录下创建字体文件夹。然后将您的字体复制并粘贴到字体文件夹中。现在只需添加fontFamily
到EditText标记 xml。
like below.
像下面。
<EditText
android:id="@+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/headerShare"
android:layout_marginBottom="5dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@drawable/edittext_bg"
android:fontFamily="@font/ritaric"
android:gravity="start"
android:hint="Subject"
android:inputType="text"
android:maxHeight="50dp"
android:padding="16dp"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="@dimen/colors_textview_size" />
We use android:fontFamily="@font/ritaric"
for apply font on EditText.
我们android:fontFamily="@font/ritaric"
用于在 EditText 上应用字体。
回答by Ishtiaq
void setFont(Context context, ViewGroup vg)
{
final String FONT_NAME = "lato_bold.ttf";
for (int i = 0; i < vg.getChildCount(); i++)
{
View v = vg.getChildAt(i);
if (v instanceof ViewGroup)
setFont(context, (ViewGroup) v);
else if (v instanceof TextView)
{
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
}
else if (v instanceof EditText)
{
((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
}
else if (v instanceof Button)
{
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
}
else if (v instanceof CheckBox)
{
((CheckBox) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
}
else if (v instanceof RadioButton)
{
((RadioButton) v).setTypeface(Typeface.createFromAsset(context.getAssets(), FONT_NAME ));
}
}
}
In your Activity or Fragment , get the main layout
在您的 Activity 或 Fragment 中,获取主布局
Inflater inflater = (Inflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//For Activity
//Inflater inflater = (Inflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.main_fragment, container, false);
//For Activity
//View v = inflater.inflate(R.layout.signup_fragment, null, false);
if (v instanceof ViewGroup)
{
setFont(getActivity(), (ViewGroup) v);
//For Activity
//setFont(this, (ViewGroup) v);
}
回答by Sunil
First Method
第一种方法
use this code in your java class
在您的 Java 类中使用此代码
EditText et_brand=(EditText)findViewById(R.id.et_brand);
et_brand.setTypeface(Typeface.createFromAsset(getAssets(),"Aspergit Bold.ttf"));
//Aspergit Bold.ttf is Font style name of text
Second Method
第二种方法
Create a new java class and it as u want
public class CustomTextView extends TextView { public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomTextView(Context context) { super(context); init(); } private void init() { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Aspergit Bold.ttf"); setTypeface(tf); } }
Use it in your Xml file
<YourPackageNAme.CustomEditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null"/>
根据需要创建一个新的java类
public class CustomTextView extends TextView { public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomTextView(Context context) { super(context); init(); } private void init() { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Aspergit Bold.ttf"); setTypeface(tf); } }
在您的 Xml 文件中使用它
<YourPackageNAme.CustomEditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null"/>
回答by Manasranjan
Add this code in your java fileWhich type you want you can add like NORMAL, ITALIC, BOLD, BOLD_ITALIC.
将此代码添加到您的 java 文件中您可以添加哪种类型,例如 NORMAL、ITALIC、BOLD、BOLD_ITALIC。
edittext.setTypeface(null, Typeface.ITALIC);
edittext.setTypeface(null, Typeface.ITALIC);
回答by Makarand
lil late but, Simple way
有点晚但是,简单的方法
mEditTextMobileNumber.setTextSize(16.0f);
if you want to change font while typing :
如果您想在键入时更改字体:
// Change text size when started entering number
mEditTextMobileNumber.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s == null || String.valueOf(s).equalsIgnoreCase("")) {
mEditTextMobileNumber.setTextSize(12.0f);
} else {
mEditTextMobileNumber.setTextSize(20.0f);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});