我可以有一个scrollView,它有很多relativeLayouts

时间:2013-08-02 13:19:33

标签: android scrollview

我正在编写一个活动,其顶部有一个linearLayout,后半部分有一个scrollView。 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个彼此相邻的editTexts。

3 个答案:

答案 0 :(得分:6)

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

 <ScrollView ...

     <LinearLayout ...

         <RelativeLayout1 ... />

         <RelativeLayout2 ... />

      </LinearLayout>
  </ScrollView>

但最简单的方法是使用ListView小部件,它会重复使用您的视图

答案 1 :(得分:0)

如果你想在布局中添加那么多的视图,我建议使用ListView,因为适配器将处理滚动和重用视图,因为在布局中有30个布局和3个editexts,因此内存效率不高。

但是如果你想使用scrollView。

ScrollView只能有一个直接子项。 所以你可以拥有这样的东西。

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

答案 2 :(得分: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);

多数民营!!!!!