Я пытаюсь отформатировать свой код, чтобы получить максимальную производительность и избежать утечки памяти и задержки работы моего приложения, одного из видов деятельности, связанных с множественным текстовым представлением, созданным программным способом (40 абзацев текста, разделенных разделителем).Улучшить формат кода (производительность)
и как мой мало знаний андроида развития я достичь ниже кода, все TextView имеет такой же цвет текста, одинаковый размер шрифта и тот же пользовательский шрифт и гравитацию, но они различны в строке только, которые настроены HTML-теги в строке XML ,
ниже код:
Есть лучше конструкция форматирования кода, чтобы получить ту же цель?
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;
}}
}
UPDATE: Стараюсь сделал это так, но я имел силы близко:
MainActivity:
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); }
}
}
FontFactory:
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;
}}
LogCat:
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
Как насчет списков и петель? – jlordo
@jlordo, пожалуйста, вы можете объяснить с помощью кода, спасибо – androidqq6
Я вижу, что у вас есть тонкие изменения на каждом TextView. Это будет не так просто. – jlordo