1

我正在编写一个顶部有一个线性布局的活动,而后半部分有一个滚动视图。scrollView 在运行时可以获得多达 30 个 relativeLayouts。这可能吗?我已经尝试使用以下代码,但它给出了一个错误:

public void onFocusChange(View v, boolean hasFocus) {
    // TODO Auto-generated method stub
    if(hasFocus==false){
    switch(v.getId()){

    case 6:

        main[no]=new RelativeLayout(this);
        mainParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
        main[no].setLayoutParams(mainParams);
        mainLayout.addView(main[no]);
        svParams=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        sv.setLayoutParams(svParams);

        items[no]=new EditText(this);
        rates[no]=new EditText(this);
        quants[no]=new EditText(this);

        items[no].setHint("Enter item name");
        rates[no].setHint("Rate");
        quants[no].setHint("Quantity");

        RelativeLayout.LayoutParams etParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rateParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams quantParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        items[no].setId(id++);
        quantParams.addRule(RelativeLayout.RIGHT_OF, (id-1));
        quants[no].setId(id++);
        pos=id;
        rateParams.addRule(RelativeLayout.RIGHT_OF, (id-1));
        rates[no].setId(id++);          

        etParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        items[no].setLayoutParams(etParams);
        rates[no].setLayoutParams(rateParams);
        quants[no].setLayoutParams(quantParams);

        rates[no].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        quants[no].setInputType(InputType.TYPE_CLASS_NUMBER);
        main[no].addView(items[no]);
        main[no].addView(rates[no]);
        main[no].addView(quants[no]);
        quants[no].setOnFocusChangeListener(this);
        sv.addView(main[no]);
        no++;
        break;

每个相对布局包含 3 个彼此相邻的 editText。

4

3 回答 3

6

ScrollView 只能有一个孩子。添加LinearLayout,它将包含您所有的RelativeLayouts并将LinearLayout添加到ScrollView

 <ScrollView ...

     <LinearLayout ...

         <RelativeLayout1 ... />

         <RelativeLayout2 ... />

      </LinearLayout>
  </ScrollView>

但最简单的方法是使用 ListView 小部件,这将重用您的视图

于 2013-08-02T13:24:50.540 回答
0

If you want to add that many views in a layout, i would suggest using a ListView because the adapter will handle scrolling and reusing views as it is not so memory efficient to have 30 layouts with 3 editexts in a layout.

But if you want to use a scrollView.

ScrollView can have only one direct child. So you can have something like this.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <!--Add your layouts here Like this -->
                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="wrap_content">
                </RelativeLayout>
                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="wrap_content">
                </RelativeLayout>
                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="wrap_content">
                </RelativeLayout>
        </LinearLayout>
    </ScrollView>
于 2013-08-02T13:30:45.103 回答
0

对于这个 U 使用 Layout Inflater Like

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

<LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/main_layout_id">

    </LinearLayout >

    </ScrollView>

作为主视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/layout_item_id">

    <EditText android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is the inflated text :D"
              android:layout_gravity="center"
              android:gravity="center_horizontal"
              android:id="@+id/text_item_id"/>
   <EditText android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is the inflated text :D"
              android:layout_gravity="center"
              android:gravity="center_horizontal"
              android:id="@+id/text_item_id"/>
  <EditText android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello, this is the inflated text :D"
              android:layout_gravity="center"
              android:gravity="center_horizontal"
              android:id="@+id/text_item_id"/>

</RelativeLayout>

这是您的相对布局,包含三个文本,将其保存为 layout_item.xml

最后在 mainlayout

你在那个开关盒之后使用

    RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.main_layout_id);
//Here u can use for loop or something else to repleat the layout
            View view = getLayoutInflater().inflate(R.layout.layout_item, mainLayout,false);


            mainLayout.addView(view);

而已 !!!!!

于 2013-08-02T13:34:27.750 回答