Android 在小部件中使用带有EditText的字体

Android 在小部件中使用带有EditText的字体,android,android-widget,typeface,Android,Android Widget,Typeface,是否可以在widget中使用从资产加载的EditText字体定义来使用Typeface editText.setTypeface(Typeface.SERIF); 假设您有这种文件结构: /assets/fonts/myfont.ttf请参阅下面的代码,它将解决您的问题 // text view label TextView mTextView1 = (TextView) findViewById(R.id.TextView1); // Loading Font Face Typeface

是否可以在widget中使用从资产加载的
EditText
字体定义来使用
Typeface

editText.setTypeface(Typeface.SERIF);
假设您有这种文件结构:


/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);
有关更多信息,请参阅下面的链接


您要使用的字体需要位于assets/fonts目录中,您可以这样访问它:

Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);

我现在已经试过了。这对我有用。祝您好运

实现此功能并避免将字体添加到所有文本视图的其他更好的表单是扩展文本视图(或EditText或…),并在setTypeface方法上应用字体。使用此方法可以控制粗体、斜体和其他样式

下面是一个扩展TextView并应用Roboto字体的类的代码。此外,它还控制了Android 4.0在html代码中设置Spanable时遇到的一些错误

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());
    }
}
}
公共类TextViewRoboto扩展了TextView{
公共静态最终字符串TAG=“TextViewRoboto”;
public TextViewRoboto(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
公共文本ViewRoboto(上下文、属性集属性){
超级(上下文,attrs);
}
公共文本ViewRoboto(上下文){
超级(上下文);
}
@凌驾
公共字体(tf字体,int样式){
//这是为了覆盖eclipse错误消息
如果(!super.isInEditMode()){
if(style==Typeface.BOLD)
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),“font/Roboto Bold.ttf”);
else if(style==Typeface.ITALIC)
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),“font/Roboto Italic.ttf”);
其他的
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),“font/Roboto Regular.ttf”);
}    
}
//
//用这段代码解决了果冻豆的and问题
//
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
试一试{
超级测量(宽度测量、高度测量);
}捕获(阵列索引边界外异常e){
//w(标记“测量问题.设置正常文本”);
setText(getText().toString());
超级测量(宽度测量、高度测量);
}
}   
@凌驾
公共空间重力(内部重力){
试一试{
超重力(重力);
}捕获(阵列索引边界外异常e){
//Logger.w(标记“Problem setGravity.Set normal text”);
setText(getText().toString());
超重力(重力);
}
}
@凌驾
public void setText(CharSequence text,BufferType){
试一试{
super.setText(文本,类型);
}捕获(阵列索引边界外异常e){
//Logger.w(标记“setText.Set normal text上的问题”);
setText(text.toString());
}
}
public void setHTMLText(字符序列文本,缓冲类型){
字符串tmpText=text.toString();
if(android.os.Build.VERSION.SDK_INT”,”);
tmpText=tmpText.replace(“”,”);
tmpText=tmpText.replace(“,”);
tmpText=tmpText.replace(“,”);
text=tmpText;
}
试一试{
super.setText(Html.fromHtml(tmpText),type);
}捕获(阵列索引边界外异常e){
//Logger.w(标记“setText.Set normal text上的问题”);
setText(text.toString());
}
}
}

你为此厌倦了什么吗?我是通过将字体转换为位图来完成的,但这很痛苦。我曾尝试在小部件的布局中设置自定义
EditText
,但没有成功(即使是简单的
CustomEditText扩展EditText
,也没有内容)。请正确阅读问题。他需要来自assetsTypeFace的定制字体提供字体,不是吗?正如@Andro Selva在另一个答案中所问的那样-它与小部件一起工作吗?您是否与小部件一起使用过?我的意思是
AppWidgetProvider
no,但是你试过这样做吗。如果是这样,这对你不起作用吗?
 Typeface tf   =    Typeface.createFromAsset(getAssets(),"fonts/Comic.ttf");
 youredittext.setTypeface(tf);
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());
    }
}
}