Android RelativeLayout问题

时间:2011-09-20 09:28:35

标签: android relativelayout

我对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>

我无法理解,为什么第一排被打破而第五排没有,我怎么能解决它...如果可以,请帮助我,谢谢!

Edit1:我没有提到我想以编程方式编辑Edit2:我改变了链接,我希望它适用于所有人

1 个答案:

答案 0 :(得分:1)

我建议您使用XML布局文件。设置正确的布局,然后连接非布局的东西,如点击处理程序或稍后更改文本。