简体   繁体   English

Android-ScrollView在相对布局上不起作用

[英]Android - ScrollView doesnt work on Relative Layout

I put a scrollview around a Relative Layout that I am adding to dynamically. 我将滚动视图放在要动态添加的相对布局周围。 If I set the height of the Relative Layout to a fixed amount that goes off of the page the scrollview will work. 如果我将“相对布局”的高度设置为固定值,该高度将离开页面,则滚动视图将起作用。

Example: 例:

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

But I need the RelativeLayout to be able to hold however many items I add to it dynamically o I set The relativelayout height to wrap_content. 但是我需要RelativeLayout能够容纳动态添加到其中的许多项目,或者将相对布局高度设置为wrap_content。 But once I add enough items to the relative layout so that they are going off the screen the Scrollview doesnt register 但是一旦我在相对布局中添加了足够多的项目,以便它们离开屏幕,Scrollview就不会注册

Below is how I am dynamically adding to the relative layout 下面是我如何动态添加到相对布局

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

Thanks 谢谢

Why are you using RelativeLayout? 为什么使用RelativeLayout? I'd advise switching to LinearLayout. 我建议切换到LinearLayout。

TRY THIS it will work for me 试试这个对我有用

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

change the layout_height of your RelativeLayout to match_parent . 将您的RelativeLayout的layout_height更改为match_parent

it will solve the problem. 它将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM