以编程方式创建多个线性布局和分隔符

时间:2013-07-01 17:51:23

标签: android android-linearlayout

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

但我必须复制线性布局和分隔线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);
        }
    }

2 个答案:

答案 0 :(得分:5)

由于所有改变的是TextView setText(),你可以将它作为一个带有String输入列表的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);
}

答案 1 :(得分:0)

首先,如果您使用XML定义布局而不是以编程方式添加它们会更容易。您也将从UI编辑器的优势中获益。 :)

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

也许这两个链接很有帮助: 1. http://developer.android.com/guide/topics/ui/declaring-layout.html 2. http://developer.android.com/guide/topics/ui/layout/listview.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。您可以更改分隔符的颜色,如此处所述How to change the divider color in the listview?

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

  • 此外,在您的代码中,您应该向ListView添加一个适配器,然后为您填充列表。以下是How to customize listview using baseadapter

  • 的示例

最后,既然你把关注点(设计和代码)分开了,你可以用你活动中的几行来实现你想要的东西(布局内容将在xml中,并且总体可以移动到一个单独的类中像MyAdapter.java ...)

希望有帮助...