Android - ScrollView不适用于相对布局

时间:2013-08-16 01:09:14

标签: android xml scrollview relativelayout

我在相对布局周围放置了一个滚动视图,我将动态添加它。如果我将相对布局的高度设置为离开页面的固定量,则滚动视图将起作用。

示例:

 <ScrollView 
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:fillViewport="true" >

    <RelativeLayout
        android:id="@+id/llcustomrow"
        android:layout_width="match_parent"
        android:layout_height="800dp" >
    </RelativeLayout>
</ScrollView>

但我需要RelativeLayout能够保存我动态添加的许多项目o我将relativelayout高度设置为wrap_content。但是,一旦我向相对布局添加了足够多的项目,以便它们离开屏幕,Scrollview就不会注册

以下是我如何动态添加到相对布局

LinearLayout mLinearLayout;
RelativeLayout rlcopy;
RelativeLayout[] rArray = new RelativeLayout[20];
int counter = 0;
RelativeLayout llcustomrow;
RelativeLayout.LayoutParams paramsleft; 


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mLinearLayout = (LinearLayout) inflater.inflate(
            R.layout.customworkout, container, false);

     paramsleft = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                                RelativeLayout.LayoutParams.WRAP_CONTENT);


    llcustomrow = (RelativeLayout)mLinearLayout.findViewById(R.id.llcustomrow);

    for(int i = 0;i<=rArray.length-1;i++){
        rArray[i] = (RelativeLayout)View.inflate(getActivity(), R.layout.addworkoutlayout, null); 
            paramsleft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
             paramsleft.setMargins(10, 0, 0, 0);
             rArray[i].setLayoutParams(paramsleft);

    }
    Button bAdd = (Button) mLinearLayout.findViewById(R.id.bAddExcercise);



    bAdd.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
                 rArray[counter].setY(rArray[counter-1].getY() + (rArray[counter-1].getHeight() +25));

             llcustomrow.addView(rArray[counter]);  
             counter++;

        }
    });


    return mLinearLayout;
}

由于

3 个答案:

答案 0 :(得分:0)

你为什么使用RelativeLayout?我建议切换到LinearLayout。

答案 1 :(得分:0)

尝试这对我有用

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


        <RelativeLayout
            android:id="@+id/outer_ll"
            android:layout_width="match_parent"
            android:layout_height="900dp"
            android:background="#E7D687"

           >



        </RelativeLayout>
        </ScrollView>

答案 2 :(得分:-2)

将RelativeLayout的layout_height更改为match_parent

它将解决问题。