1

在我的应用程序中。有一个活动包含多个以编程方式创建的线性布局和分隔线它运行良好

但我必须将线性布局和分隔线复制 5 次,除了两件事外,所有内容都是相同的:

1-每个线性布局都有不同的字符串。

2- 第一个分频器边距不同于其他分频器边距。

有没有更好的方法可以用更干净和更短的代码来做到这一点。

任何帮助都感激不尽 。

public class Dreams extends Activity {

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Boolean customTitleSupported =  
      requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     setContentView(R.layout.trip);  
     if (customTitleSupported) { 

  getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
  } 
         TextView tv = (TextView) findViewById(R.id.title); 
         tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
         tv.setText("Dreams");  

     LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);       
        // add text view
        TextView tv1 = new TextView(this);
        tv1.setGravity(Gravity.RIGHT);
        tv1.setTextSize(30);    
        tv1.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
        ll.addView(tv1);
        tv1.setText(Html.fromHtml(getString(R.string.dreams))); 

        ImageView divider1 = new ImageView(this);
        LinearLayout.LayoutParams lp1 = 
         new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp1.setMargins(40, 0, 40, 0);
        divider1.setLayoutParams(lp1);
        divider1.setBackgroundColor(Color.RED);
        ll.addView(divider1);

        TextView tv2 = new TextView(this);      
        tv2.setGravity(Gravity.RIGHT);
        tv2.setTextSize(30);
        tv2.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
        ll.addView(tv2);
        tv2.setText(Html.fromHtml(getString(R.string.dream_1)));

        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.RED);
        ll.addView(divider2);       

        TextView tv3 = new TextView(this);
        tv3.setGravity(Gravity.RIGHT);
        tv3.setTextSize(30);
        tv3.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
        ll.addView(tv3);
        tv3.setText(Html.fromHtml(getString(R.string.dream_2)));

        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.RED);
        ll.addView(divider3);

        TextView tv4 = new TextView(this);
        tv4.setGravity(Gravity.RIGHT);
        tv4.setTextSize(30);    
        tv4.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
        ll.addView(tv4);
        tv4.setText(Html.fromHtml(getString(R.string.dream_3)));    

        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.RED);
        ll.addView(divider4);

        TextView tv5 = new TextView(this);      
        tv5.setGravity(Gravity.RIGHT);
        tv5.setTextSize(30);
        tv5.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
        ll.addView(tv5);
        tv5.setText(Html.fromHtml(getString(R.string.dream_4)));

        ImageView divider5 = new ImageView(this);
        LinearLayout.LayoutParams lp5 = 
         new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp5.setMargins(10, 10, 10, 10);
        divider5.setLayoutParams(lp5);
        divider5.setBackgroundColor(Color.RED);
        ll.addView(divider5);

        TextView tv6 = new TextView(this);
        tv6.setGravity(Gravity.RIGHT);
        tv6.setTextSize(30);
        tv6.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
        ll.addView(tv6);
        tv6.setText(Html.fromHtml(getString(R.string.dream_5)));

        ImageView divider6 = new ImageView(this);
        LinearLayout.LayoutParams lp6 = 
         new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp6.setMargins(10, 10, 10, 10);
        divider6.setLayoutParams(lp6);
        divider6.setBackgroundColor(Color.RED);
        ll.addView(divider6);
        }
    }
4

2 回答 2

5

由于所有正在改变的是 TextView setText() 您可以将其设为带有字符串输入列表的 for 循环。例如:

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);       

String[] textEntries = {  getString(R.string.dream),
                          getString(R.string.dream_1),
                          getString(R.string.dream_2),
                          getString(R.string.dream_3),
                          getString(R.string.dream_4),
                          getString(R.string.dream_5)
                        };

for ( int i = 0; i < textEntries.length; i++)
{
    TextView tv = new TextView(this);
    tv.setGravity(Gravity.RIGHT);
    tv.setTextSize(30);
    tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
    ll.addView(tv);
    tv.setText(Html.fromHtml(textEntries[i]));

    ImageView divider = new ImageView(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
    lp.setMargins(10, 10, 10, 10);
    divider.setLayoutParams(lp);
    divider.setBackgroundColor(Color.RED);
    ll.addView(divider);
}
于 2013-07-01T18:39:58.870 回答
0

首先,如果您在 XML 中定义布局而不是以编程方式添加它们会更容易。您还将受益于 UI 编辑器的好处。:)

其次,您可能希望使用 ListView 和 Adapter 来填充列表,因为您不想为每个布局重复相同的任务。

也许这两个链接有帮助: 1. http://developer.android.com/guide/topics/ui/declaring-layout.html 2. http://developer.android.com/guide/topics/ui/layout/列表视图.html

因此,要最终回答您的问题,我将执行以下操作:

  • 创建一个文件,例如 list_item.xml,其中包含以下内容:

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"><TextView your attributes.../></LinearLayout>
    
  • 创建另一个布局,例如 main.xml,其中包含一个 ListView。您可以更改分隔线的颜色,如此处所述如何更改列表视图中的分隔线颜色?.

  • 在您的代码(活动或片段)中,通过 setContentView() 添加 main.xml 作为内容视图。

  • 同样在您的代码中,您应该向 ListView 添加一个适配器,然后它会为您填充列表。这是一个示例如何使用 baseadapter 自定义列表视图

最后,由于您将关注点(设计和代码)分开,您只需在活动中的几行代码就可以实现您想要的(布局内容将在 xml 中,并且可以将总体移动到一个单独的类,如 MyAdapter.xml )。爪哇...)

希望有帮助...

于 2013-07-01T18:24:13.210 回答