是否可以使用某種方式Typeface
字體定義從窗口小部件中的EditText
加載?在窗口小部件中使用帶有EditText的字體
回答
要使用需要駐留在資產/ fonts目錄,並且您可以訪問它像這樣的字體:
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);
正如@Andro Selva在另一個答案中提出的問題 - 它是否適用於小部件? – hsz
是的,它適用於我.. –
您可以使用此
editText.setTypeface(Typeface.SERIF);
正確地閱讀問題。他需要資產的自定義字體 –
TypeFace提供字體,不是嗎? –
ya。但他需要編輯文字的自定義字體 –
editText.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/myfont.ttf"));
假設你有文件的這種結構:
/assets/fonts/myfont.ttf
請參見下面的代碼是,它將解決你的問題。
// text view label
TextView mTextView1 = (TextView) findViewById(R.id.TextView1);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), "DroidSansFallback.ttf");
// Applying font
mTextView1.setTypeface(tf);
並參見下面的鏈接瞭解更多信息。
這是否適用於小工具? –
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/Comic.ttf");
youredittext.setTypeface(tf);
現在我已經嘗試過這一點。它爲我工作。祝你好運
你用它與小部件?我的意思是'AppWidgetProvider' – hsz
不,但你有沒有試過這樣的。如果是的話,它不適合你? –
其他betther形式來實現這一點,並避免將字體添加到所有textview擴展TextView(或EditText或...)並將字體應用於setTypeface方法。使用這種方法,您可以控制粗體,斜體和其他樣式。
以下是擴展TextView並應用Roboto字體的類的代碼。此外,它控制了Android 4.0與HTML代碼設置Spannable時從HTML中的一些錯誤
public class TextViewRoboto extends TextView {
public static final String TAG = "TextViewRoboto";
public TextViewRoboto(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TextViewRoboto(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewRoboto(Context context) {
super(context);
}
@Override
public void setTypeface(Typeface tf, int style) {
//This is to override eclipse error messages
if (!super.isInEditMode()) {
if (style == Typeface.BOLD)
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf"));
else if (style == Typeface.ITALIC)
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf"));
else
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
}
}
//
// With this code aboid the <b> and <strong> problem on Jelly Bean
//
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}catch (ArrayIndexOutOfBoundsException e){
//Logger.w(TAG, "Problem onMeasure. Set normal text");
setText(getText().toString());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
public void setGravity(int gravity){
try{
super.setGravity(gravity);
}catch (ArrayIndexOutOfBoundsException e){
//Logger.w(TAG, "Problem setGravity. Set normal text");
setText(getText().toString());
super.setGravity(gravity);
}
}
@Override
public void setText(CharSequence text, BufferType type) {
try{
super.setText(text, type);
}catch (ArrayIndexOutOfBoundsException e){
//Logger.w(TAG, "Problem on setText. Set normal text");
setText(text.toString());
}
}
public void setHTMLText(CharSequence text, BufferType type) {
String tmpText = text.toString();
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
tmpText = tmpText.replace("<strong>", "<b>");
tmpText = tmpText.replace("</strong>", "</b>");
tmpText = tmpText.replace("<em>", "<i>");
tmpText = tmpText.replace("</em>", "</i>");
text = tmpText;
}
try{
super.setText(Html.fromHtml(tmpText), type);
}catch (ArrayIndexOutOfBoundsException e){
//Logger.w(TAG, "Problem on setText. Set normal text");
setText(text.toString());
}
}
}
- 1. 爲什麼我沒有在我的eclipse IDE窗體小部件中使用EditText
- 2. django帶窗口小部件的動態窗體
- 3. 使用Django窗體而不使用窗口小部件
- 4. 沒有佈局的父窗口小部件上的中心窗口小部件
- 5. 如何清除父窗口小部件中的所有窗口小部件?
- 6. 如何在Windows中使用ttk主題窗口小部件更改字體
- 7. 如何在窗口中調整窗口小部件的大小
- 8. 在Gtk 3.0中基於窗口小部件大小縮放字體大小
- 9. 使用帶有窗口的套接字
- 10. 帶有窗體的大文件大小?
- 11. 如何在django窗體中使用日曆視覺窗口小部件?
- 12. 窗口小部件
- 13. 最小化和恢復窗體上的窗口小部件
- 14. 在modelformset_factory中使用具有自定義字段小部件的窗體?
- 15. 在Android窗口小部件中的GridView
- 16. 不在Gtk窗口中的小部件
- 17. 帶有窗口小部件的IE安全警告
- 18. 帶窗口的Windows窗體控件
- 19. 使用BoundList作爲窗口小部件
- 20. 使用Firebase填充窗口小部件
- 21. 使用python窗口小部件
- 22. 使用QStackedLayout創建窗口小部件
- 23. 放入CDetailView窗口小部件另一個窗口小部件
- 24. 小部件沒有出現在窗口部件列表中
- 25. 在tkinter中,您使用Excel中的哪個窗口小部件?
- 26. Django窗體中的奇怪行爲(只讀字段/窗口小部件)
- 27. 如何禁用父窗口小部件中的所有用戶輸入窗口小部件(按鈕,條目..)?
- 28. IText7僅在新文檔上創建窗體/窗口小部件
- 29. 從窗口訪問窗口小部件*
- 30. 調用窗口小部件中的另一個窗口小部件會失去所有窗口小部件的屬性
你有沒有爲此感到厭倦? –
我把它轉換成了位圖,但這很痛苦。我試圖在小部件的佈局中設置自定義的'EditText',但沒有運氣(即使簡單的'CustomEditText擴展EditText'沒有內容)。 – hsz