0

我尝试格式化我的代码以获得最佳性能并避免内存泄漏和应用程序操作延迟,这是处理以编程方式创建的多个文本视图的活动之一(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
4

2 回答 2

1

我强烈建议您考虑使用 aListView这样您就不必单独创建这些项目中的每一个。另外,我不知道你的实现FontFactory是什么,但如果可能的话,只创建一次字体。

于 2013-05-16T21:27:54.437 回答
0

如果您真的想使用TextViewwithImageView作为分隔符,您可以首先创建一个段落字符串数组。

把这个放在你的/res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="paragraphs">
        <item>Paragraph 1 content here</item>
        <item>Paragraph 2 content here</item>
        <item>Paragraph 3 content here</item>
<!-- And so on until how many paragraphs you like -->
    </string-array>
</resources>

然后,您可以像这样在代码中获取这些段落:

String[] paragraphs = getResources().getStringArray(R.array.paragraphs);

然后你可以使用这样的方法:

private void addParagraphs(LinearLayout layout, String[] paragraphs) {
    for (String paragraph : paragraphs) {
        TextView tv = new TextView(this);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(30);    
        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);
    }
}

要完成该示例,您可以使用如下段落填充布局:

String[] paragraphs = getResources().getStringArray(R.array.paragraphs);
LinearLayout layout = (LinearLayout) findViewById(R.id.yourLayout);
addParagraphs(layout, paragraphs);

编辑:如果要设置其他段落参数,如对齐方式,您可以创建另一个字符串数组,res/values/arrays.xml其项目数与段落字符串数组相同,然后将它们传递给方法以设置每个段落编号的对齐方式。有更多的方法可以做到这一点,这只是一个例子。发挥创意并享受。:)

于 2013-05-16T23:49:05.193 回答