滚动视图内的相对布局无法正常工作。

时间:2013-12-08 06:54:38

标签: android scrollview relativelayout

我在scrollview中使用了relativelayout,我正在动态地向该relativelayout添加视图。尽管视图不是屏幕大小,但它不滚动。 请帮助解决这个任务。

我的ScrollView xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    tools:context=".MainActivity" >

   <ScrollView
        android:id="@+id/mainScrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg"
        android:fillViewport="true"


        >

     <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            />


</ScrollView>
</RelativeLayout>

用于动态添加图片的代码:

     if (isOddViewNo)
                {
                    isOddViewNo=false;

                    final View view= getLayoutInflater().inflate(R.layout.sample, null);

                    iv=  (ImageView)view.findViewById(R.id.iv_small);
                ImageView   iv1=  (ImageView)view.findViewById(R.id.iv_big);
                iv.setVisibility(View.VISIBLE);
                iv1.setVisibility(View.GONE);

                iv.setImageResource(R.drawable.ic_launcher);





                    iv.setX(x-45);
                    iv.setY(ypos+17);

                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

                    view.setLayoutParams(lp);

                    rl.addView(view);
                    x+=100;
                }
               else
                {
                    isOddViewNo=true;

                    final View view= getLayoutInflater().inflate(R.layout.sample, null);


                    iv=  (ImageView)view.findViewById(R.id.iv_big);


                    ImageView   iv1=  (ImageView)view.findViewById(R.id.iv_small);
                    iv.setVisibility(View.VISIBLE);
                    iv1.setVisibility(View.GONE);
                    iv.setImageResource(R.drawable.ic_launcher);
//                  iv.setLayoutParams(new LayoutParams(200,200));

                    iv.setX(x-45);
                    iv.setY(ypos);

                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

                    view.setLayoutParams(lp);

                    rl.addView(view);

                    x+=178;
                }

my inflated sample.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<ImageView 
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:id="@+id/iv_small"

    android:visibility="gone"
    />

<ImageView 
    android:layout_height="80dp"
    android:layout_width="80dp"
    android:id="@+id/iv_big"
    android:visibility="gone"

    />
</LinearLayout>

我的屏幕截图:As I showed my screenshot,it has many images below but couldnt able to scroll.

1 个答案:

答案 0 :(得分:0)

您必须将ScrollView高度设置为wrap_content。也永远不要使用fill_parent(不建议使用),而是使用match_parent。