2013-07-01 81 views
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); 
     } 
    } 

回答

5

由於所有這種情況正在改變是的TextView的setText(),你可以使這個與循環字符串輸入列表。例如:

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); 
} 
0

首先,如果您使用XML定義佈局而不是以編程方式添加它們,那將會更容易。您也將從UI編輯器的好處中受益。 :)

其次,您可能希望使用ListView和適配器來填充列表,因爲您不希望爲每個佈局複製相同的任務。

也許這兩個環節是有幫助的: 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分離類...)

希望幫助...

+0

謝謝您的回答,首先關閉所有,ID喜歡有它在安裝插件爲:一個線性佈局和一個分頻器和一個類,像creationlayoutwithdivider類,在另一方面,對於列表視圖每個項目文字會有所不同,必須從字符串中獲取。xml作爲它的長文本並且用html標籤定製每個字符串,所以如何從string.xml獲得listview項目字符串,再次感謝 – androidqq6

+0

可以定義一個字符串數組http://developer.android.com/guide/topics/resources/字符串resource.html#字符串數組。如果你想在你的資源中定義HTML,你應該考慮http://stackoverflow.com/questions/4490073/xml-within-an-android-string-resource。最後,這個例子可能會幫助你http://stackoverflow.com/a/2395270/2538428。 – a11n

相關問題