gpt4 book ai didi

android - 如何通过 xml 而不是通过 java 代码设置自定义(arial)类型的面孔

转载 作者:行者123 更新时间:2023-11-30 04:43:27 25 4
gpt4 key购买 nike

我已经意识到这一点:-字体 typefaceArial= Typeface.createFromAsset(context.getAssets(), "arial.ttf");

但是当我创建以下类时,它可以工作,但会出现低内存警告问题。

公共(public)类 MidColorTextView 扩展了 TextView{

private CharSequence text;
private String token;
private static Context context;
private String colorSpan;
private int colorCode;
private static Typeface typefaceArial;

public MidColorTextView( Context context , AttributeSet attrs )
{
super(context, attrs);
this.context=null;
this.context = context;


for(int i = 0; i < attrs.getAttributeCount(); i ++ )
{
// Log.i(TAG, attrs.getAttributeName(i));
/*
* Read value of custom attributes
*/

this.text = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.lht", "text");

this.token = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.lht", "token");
this.colorSpan = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.lht", "colorSpan");
// Log.i("TAG", "token " + token);
// Log.i("TAG", "text " + text);
// Log.i("TAG", "colorSpan " + colorSpan);

}
init();
}

private void init ()
{
if(text.charAt(0) == '@')
{
String tempText = (String) text.subSequence(1, text.length());
this.text = Html.fromHtml(getResources().getString(Integer.parseInt(tempText)));

}
if(token.charAt(0) == '@')
{
String tempText = (String) token.subSequence(1, token.length());
this.token = getResources().getString(Integer.parseInt(tempText));
}
if(colorSpan.charAt(0) == '@')
{
String tempText = (String) colorSpan.subSequence(1, colorSpan.length());
this.colorSpan = getResources().getString(Integer.parseInt(tempText));
}

setColorCode(Color.parseColor(colorSpan));

CharSequence textWitoutToken = null;
String tempString = text.toString();

// ---------checking whether text containg token or not.
if(tempString.contains(token))
{
textWitoutToken = setSpanBetweenTokens(text, token, new ForegroundColorSpan(colorCode));
}
else
{
textWitoutToken = text;
}
textContent = null;
setText(textWitoutToken);
setTypefaceArial ();
setTypeface(getTypefaceArial ());

}



public int getColorCode ()
{
return colorCode;
}

public void setColorCode ( int colorCode )
{
this.colorCode = colorCode;
}

private CharSequence textContent;


public static Typeface getTypefaceArial ()
{
return typefaceArial;
}
public static void setTypefaceArial ()
{
MidColorTextView.typefaceArial= Typeface.createFromAsset(context.getAssets(), "arial.ttf");
}

最佳答案

我通过使用单例类解决了这个问题。我给出了完整的代码,以便它可以帮助其他人。

1. 在 XML 中定义:

    xmlns:lht="http://schemas.android.com/apk/res/com.lht"     android:id="@+id/basicLayout"

<com.xyz.util.MidColorTextView
xyz:token="#"
xyz:colorSpan="@color/BrightRed"
xyz:text="@string/AppraisingStaffBottomText_imanage"
style="@style/contentDescriptionText" />

2. 创建类MidColorTextView

    package com.xyz.util;

public class MidColorTextView extends TextView {

private CharSequence text;
private String token;
private Context context;
private String colorSpan;
private int colorCode;


public MidColorTextView( Context context , AttributeSet attrs ) {
super(context, attrs);
this.context = context;


for(int i = 0; i < attrs.getAttributeCount(); i ++ ) {
// Log.i(TAG, attrs.getAttributeName(i));
/*
* Read value of custom attributes
*/

this.text = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.xyz", "text");

this.token = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.xyz", "token");
this.colorSpan = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.xyz", "colorSpan");
// Log.i("TAG", "token " + token);
// Log.i("TAG", "text " + text);
// Log.i("TAG", "colorSpan " + colorSpan);

}
init();
}

private void init () {

if(text.charAt(0) == '@') {
String tempText = (String) text.subSequence(1, text.length());
this.text = Html.fromHtml(getResources().getString(Integer.parseInt(tempText)));

}
if(token.charAt(0) == '@') {
String tempText = (String) token.subSequence(1, token.length());
this.token = getResources().getString(Integer.parseInt(tempText));
}
if(colorSpan.charAt(0) == '@')
{
String tempText = (String) colorSpan.subSequence(1, colorSpan.length());
this.colorSpan = getResources().getString(Integer.parseInt(tempText));
}

setColorCode(Color.parseColor(colorSpan));

CharSequence textWitoutToken = null;
String tempString = text.toString();

// ---------checking whether text containg token or not.
if(tempString.contains(token))
{
textWitoutToken = setSpanBetweenTokens(text, token, new ForegroundColorSpan(colorCode));
}
else
{
textWitoutToken = text;
}
textContent = null;
setText(textWitoutToken);
setTypeface(FontManager.getInstance(context).getTypefaceArial ());

}

public void setText ( CharSequence text , String token , int color )
{
setText(setSpanBetweenTokens(text, token, new ForegroundColorSpan(color)));

setTypeface(FontManager.getInstance(context).getTypefaceArial ());

}

public int getColorCode ()
{
return colorCode;
}

public void setColorCode ( int colorCode )
{
this.colorCode = colorCode;
}

private CharSequence textContent;

public CharSequence setSpanBetweenTokens ( CharSequence text , String token , CharacterStyle... cs )
{
// Start and end refer to the points where the span will apply
int tokenLen = token.length();
int start = text.toString().indexOf(token) + tokenLen;
int end = text.toString().indexOf(token, start);
if(start > - 1 && end > - 1)
{
// Copy the spannable string to a mutable spannable string
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
for(CharacterStyle c : cs)
{
ssb.setSpan(c, start, end, 0);
}
// Delete the tokens before and after the span
ssb.delete(end, end + tokenLen);
ssb.delete(start - tokenLen, start);

text = ssb;
textContent = ssb;
String tempString = textContent.toString();
if(tempString.contains(token))
{
setSpanBetweenTokens(textContent, token, new ForegroundColorSpan(colorCode));
}

}

return textContent;
}

}

3. 创建类FontManager

public class FontManager {
private Typeface typefaceArial;
private Context context;

private static FontManager instance = null;

private FontManager(Context context) {
this.context = context;
this.typefaceArial= Typeface.createFromAsset(context.getAssets(), "arial.ttf");
}

public synchronized static FontManager getInstance(Context context) {
if(instance == null) {
instance = new FontManager(context);
}
return instance;
}

public Typeface getTypefaceArial () {
return typefaceArial;
}

}

这将解决您所有的问题。

setSpanBetweenTokens 用于特定标记之间的颜色文本。

这是一个要测试的字符串资源:

<string name="AppraisingStaffBottomText_imanage">The meeting&lt;br>&lt;br>PAST&lt;br>Allow the employee to give you
their view of their positive
progress over the past period, focus them on this with open
questions, such as:&lt;br>&lt;i>#\"What has been your important contribution over the past
6
months?\"#&lt;/i>&lt;br>&lt;i>#\"What have your learned about your role?\"#&lt;/i>&lt;br>
&lt;i>#\"What has been your important success?\"#&lt;/i>&lt;br>Don\'t rake over past mistakes,
don\'t focus on poor performance
- you cannot change that, reserve those discussions future
development - see below&lt;br>&lt;br>PRESENT&lt;br>Using open questions, help staff to identify
their true strengths,
capabilities, attributes, skills and attitudes. Create a
comprehensive picture of them as a strategic contributor and
resource.&lt;br>What are your skills, and to what level?&lt;br>&lt;i>#\"What have you added as
capabilities over the past months?\"#&lt;/i>&lt;br>&lt;i>#\"What do you find are your
most useful personal attributes in
your role?\"#&lt;/i>&lt;br>&lt;br>FUTURE&lt;br>The future is the period where changes in
capability and
performance can be made.&lt;br>This discussion is where your people can figure out -
with your
help - what development they need to reach your performance
standards and their career goals. It begins with understanding
their career goals, so ....&lt;br>&lt;i>#\"What are your goals?\"#&lt;/i>&lt;br>&lt;i>#\"What
development will be needed?\"#&lt;/i>
&lt;br>&lt;i>#\"You have seen over the past months that you may need more skill in these
areas .......................... what should we do about
that?\"#&lt;/i>&lt;br>&lt;br>Finally: Agree a specific development plan that includes
training/experience in the areas where more skill is needed. Fix dates
in the diary&lt;br>&lt;br>The Manager\'s role is one of Mentor and Guide; not
Judge and
Jury</string>

关于android - 如何通过 xml 而不是通过 java 代码设置自定义(arial)类型的面孔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5541058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com