我尝试格式化我的代码以获得最佳性能并避免内存泄漏和应用程序操作延迟,这是处理以编程方式创建的多个文本视图的活动之一(40 个文本视图段落由分隔符分隔)。
并且作为我对android开发的一点了解,我达到了以下代码,所有textview都具有相同的文本颜色,相同的文本大小以及相同的自定义字体和重力,但它们仅在字符串中不同,由字符串xml中的html标签自定义。
如下代码:
是否有更好的建筑代码格式来获得相同的目的?
public class Text extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitleSupported = requestWindowFeature (Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.text);
if (customTitleSupported) { getWindow().setFeatureInt
(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); }
TextView tv = (TextView) findViewById(R.id.title_tv1);
Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");
tv.setTypeface(face);
tv.setText(" My Text ");
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.CENTER);
tv1.setTextSize(30);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
ll.addView(tv1);
tv1.setText(Html.fromHtml(getString(R.string.text1)));
ImageView divider1 = new ImageView(this);
LinearLayout.LayoutParams lp1 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp1.setMargins(10, 10, 10, 10);
divider1.setLayoutParams(lp1);
divider1.setBackgroundColor(Color.BLUE);
ll.addView(divider1);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.RIGHT);
tv2.setTextSize(30);
tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));
ll.addView(tv2);
tv2.setText(Html.fromHtml(getString(R.string.TEXT2)));
ImageView divider2 = new ImageView(this);
LinearLayout.LayoutParams lp2 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp2.setMargins(10, 10, 10, 10);
divider2.setLayoutParams(lp2);
divider2.setBackgroundColor(Color.BLUE);
ll.addView(divider2);
TextView tv3 = new TextView(this);
tv3.setGravity(Gravity.CENTER);
tv3.setTextSize(30);
tv3.setTypeface(FontFactory.getBFantezy(getBaseContext()));
ll.addView(tv3);
tv3.setText(Html.fromHtml(getString(R.string.TEXT3)));
ImageView divider3 = new ImageView(this);
LinearLayout.LayoutParams lp3 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp3.setMargins(10, 10, 10, 10);
divider3.setLayoutParams(lp3);
divider3.setBackgroundColor(Color.BLUE);
ll.addView(divider3);
TextView tv4 = new TextView(this);
tv4.setGravity(Gravity.CENTER);
tv4.setTextSize(30);
tv4.setTypeface(FontFactory.getBFantezy(getBaseContext()));
ll.addView(tv4);
tv4.setText(Html.fromHtml(getString(R.string.TEXT4)));
ImageView divider4 = new ImageView(this);
LinearLayout.LayoutParams lp4 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp4.setMargins(10, 10, 10, 10);
divider4.setLayoutParams(lp4);
divider4.setBackgroundColor(Color.BLUE);
ll.addView(divider4);
这将以相同的方式持续到 textview tv40 。
添加自定义字体如下:
public static class FontFactory {
private static Typeface t1;
public static Typeface getBFantezy(Context c) {
if (t1 == null) {
t1 = Typeface.createFromAsset(c.getAssets(), "BFantezy.ttf");
}
return t1;
}}
}
更新: 我尝试这样做,但我强制关闭:
主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] paragraphs = getResources().getStringArray(R.array.paragraphs);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
addParagraphs(layout, paragraphs);
setContentView(R.layout.main);}
private void addParagraphs(LinearLayout layout, String[] paragraphs) {
for (String paragraph : paragraphs) {
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(30);
tv.setTextColor(Color.GREEN);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
layout.addView(tv);
tv.setText(paragraph);
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
params.setMargins(10, 10, 10, 10);
divider.setLayoutParams(params);
divider.setBackgroundColor(Color.BLUE);
layout.addView(divider); }
}
}
字体工厂:
public class FontFactory {
private static Typeface t1;
public static Typeface getBFantezy(Context c) {
if (t1 == null) {
t1 = Typeface.createFromAsset(c.getAssets(), "BFantezy.ttf");
}
return t1;
}}
日志猫:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.test.demo/com.test.demo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.test.demo.MainActivity.addParagraphs(MainActivity.java:29)
at com.test.demo.MainActivity.onCreate(MainActivity.java:18)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more