ScrollView不在Android中滚动

时间:2014-06-22 20:12:14

标签: android android-activity imageview relativelayout android-scrollview

我正在尝试使用for循环将图像添加到我的相对布局中,但它不起作用。 我试图删除for循环并使一个图像更长,滚动视图似乎工作,但当我尝试添加多个图像到相对layou它添加它但我不能滚动它们。 这意味着我可以看到有5张图片,但我只能看到它的上半部分而且无法滚动查看下半部分。

这是我用于活动的xml。

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/MainRelativeLayout"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context="com.tutecentral.tvtimeschedule.app.MainActivity"
            android:background="#87CABE"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

</RelativeLayout>
</ScrollView>
</LinearLayout>

这是我用来添加图片的活动

        int amountOfChannels = 9;
        int paddingTopOfImage = 0;

        for(int i = 1; i <= amountOfChannels; i++) {

            //ImageView Setup
            ImageView imageView = new ImageView(this);
            //setting image resource
            imageView.setImageResource(R.drawable.channel_1);
            //setting image position

            imageView.setLayoutParams(new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT));

            imageView.getLayoutParams().height = 400;
            imageView.getLayoutParams().width = 1000;

            imageView.setPadding(10, 10, 10, 10);
            imageView.setBackgroundColor(Color.WHITE);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setY(paddingTopOfImage);

            //adding view to layout
            RelativeLayout mainRelativeLayout = (RelativeLayout) findViewById(R.id.MainRelativeLayout);
            mainRelativeLayout.addView(imageView);

            paddingTopOfImage = paddingTopOfImage + 450;

        }

1 个答案:

答案 0 :(得分:0)

我认为您不必将ScrollView放在LinearLayout下,因为默认情况下ScrollView是LinearLayout。尝试删除LinearLayout。