2011-09-20 79 views
0

我有一些問題的RelativeLayout定位:的Android RelativeLayout的問題

現在我有這樣的:http://dl.dropbox.com/u/18411570/now.png ,我想這樣的:http://dl.dropbox.com/u/18411570/expectation.png

這裏是我的代碼:

public class ActivityTwo extends Activity { 

    RelativeLayout myRelativeLayout = null; 

    ArrayList<ArrayList<EditText>> spreadsheet = new ArrayList<ArrayList<EditText>>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.relativesprsheet); 

     myRelativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout); 

     //FIRSTROW 

     ArrayList<EditText> firstRow = new ArrayList<EditText>(); 

     char columnTitle = 'A'; 

     int prev_id = 0; 

     for(int i = 0; i < 5; ++i) { 
      EditText eText = new EditText(this); 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      eText.setWidth(100); 
      eText.setId(getUniqueCellId(columnTitle,i + 1)); 
      if(i==0) { 
       params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,eText.getId()); 
       eText.setText("A1"); 
      } 
      else { 
       params.addRule(RelativeLayout.RIGHT_OF,prev_id); 
       eText.setText(Character.toString(columnTitle)+Integer.toString(1)); 
      } 
      prev_id = getUniqueCellId(columnTitle,i + 1); 
      firstRow.add(eText); 
      myRelativeLayout.addView(eText,params); 
      columnTitle++; 
     } 

     spreadsheet.add(firstRow); 

     //SECONDROW 

     ArrayList<EditText> second_row = new ArrayList<EditText>(); 
     EditText a2 = new EditText(this); 
     a2.setId(getUniqueCellId('A',2)); 
     a2.setWidth(100); 
     a2.setText("A2"); 
     RelativeLayout.LayoutParams a2params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     a2params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 1)); 
     myRelativeLayout.addView(a2, a2params); 
     second_row.add(a2); 

     EditText bigView = new EditText(this); 
     bigView.setId(getUniqueCellId('B', 2)); 
     bigView.setWidth(300); 
     bigView.setText("B2"); 
     RelativeLayout.LayoutParams b2params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     b2params.addRule(RelativeLayout.RIGHT_OF, getUniqueCellId('A', 2)); 
     b2params.addRule(RelativeLayout.ALIGN_TOP,getUniqueCellId('A', 2)); 
     b2params.addRule(RelativeLayout.ALIGN_BOTTOM,getUniqueCellId('A', 4)); 
     b2params.addRule(RelativeLayout.ALIGN_RIGHT,getUniqueCellId('D',5)); 
     myRelativeLayout.addView(bigView, b2params); 
     second_row.add(bigView); 

     spreadsheet.add(second_row); 

     //THIRDROW 

     ArrayList<EditText> third_row = new ArrayList<EditText>(); 
     EditText a3 = new EditText(this); 
     a3.setId(getUniqueCellId('A',3)); 
     a3.setWidth(100); 
     a3.setText("A3"); 
     RelativeLayout.LayoutParams a3params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     a3params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 2)); 
     myRelativeLayout.addView(a3, a3params); 
     third_row.add(a3); 
     spreadsheet.add(third_row); 

     //FOURTHROW 

     ArrayList<EditText> fourth_row = new ArrayList<EditText>(); 
     EditText a4 = new EditText(this); 
     a4.setId(getUniqueCellId('A',4)); 
     a4.setWidth(100); 
     a4.setText("A4"); 
     RelativeLayout.LayoutParams a4params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     a4params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 3)); 
     myRelativeLayout.addView(a4, a4params); 
     fourth_row.add(a4); 
     spreadsheet.add(fourth_row); 

     //FIFTHROW 

     ArrayList<EditText> fifth_row = new ArrayList<EditText>(); 
     EditText a5 = new EditText(this); 
     a5.setId(getUniqueCellId('A',5)); 
     a5.setWidth(100); 
     a5.setText("A5"); 
     RelativeLayout.LayoutParams a5params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     a5params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 4)); 
     myRelativeLayout.addView(a5, a5params); 
     fifth_row.add(a5); 

     char c2 = 'B'; 
     for(int i = 1; i < 5; ++i) { 
      EditText temp5Text = new EditText(this); 
      temp5Text.setId(getUniqueCellId(c2,5)); 
      temp5Text.setWidth(100); 
      temp5Text.setText(Character.toString(c2)+"5"); 
      RelativeLayout.LayoutParams temp5Textparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
      temp5Textparams.addRule(RelativeLayout.RIGHT_OF, getUniqueCellId((char)(c2-1), 5)); 
      temp5Textparams.addRule(RelativeLayout.ALIGN_TOP, getUniqueCellId((char)(c2-1), 5)); 
      myRelativeLayout.addView(temp5Text, temp5Textparams); 
      fifth_row.add(temp5Text); 
      c2++; 
     } 

     spreadsheet.add(fifth_row); 

    } 

    private int getUniqueCellId(char c, int i) { 
     return Integer.valueOf(c) * 10369 + i; 
    } 

} 

這裏是佈局xml:

<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 
     <RelativeLayout 
      android:id="@+id/relativeLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     </RelativeLayout> 
    </ScrollView> 
</HorizontalScrollView> 

我不明白,爲什麼第一行被打破,第五行不行,我該如何修復它......請幫助我,如果可以的話,thx!

EDIT1:我沒有提到,我想這樣做編程EDIT2:我改變了聯繫,我希望它會爲大家

+0

請將您的圖片附加到您的代碼中不要發送鏈接我無法查看它的訪問被拒絕 –

+0

setGravity在第一行EditText的左邊,而不是嘗試。 –

+0

我改變了鏈接,setGravity不起作用 – viplezer

回答

1

我建議你用你的XML佈局文件對這項工作。設置好它們的佈局,然後連接非佈局的東西,比如點擊處理程序或稍後更改文本。

+0

這不是一個好的答案,因爲我不得不用din生成UI,但這是我接受它的唯一答案。我解決了這個問題,這個問題與ID有關。 – viplezer

+0

Coelho,當我需要動態生成佈局(例如,填充列表)時,我把它們生成的每個部分放在它自己的佈局中(the_list.xml,item_type_one.xml,item_type_two,xml)。然後,當我添加它們時,我從這些佈局中充氣物品。當我需要更改或修復佈局時,可以更容易地看到發生了什麼。只是一個建議。 –

+0

這是一個電子表格應用程序,我解壓縮並解析了一個odt文件,並使用EditText實例生成了電子表格。我看到你的方法,但在這種情況下,我認爲這不是有利的。無論如何,感謝四種方法,我會嘗試,當我可以:) – viplezer