ScrollView用于整个界面

时间:2015-09-24 05:49:01

标签: android uiscrollview horizontalscrollview vertical-scrolling

我已经实现了一个带有两个滚动视图的界面 - 一些imageViews的Horizo​​ntalScrollView和几个textViews的垂直scrollView。现在我想要一个垂直scrollView用于整个界面。我怎样才能做到这一点?请帮我。这是接口的.xml文件: -

<LinearLayout 
    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:orientation="vertical"
    tools:context="com.example.rahulshaw.myfragmentsexample.HomeFragment">

<!-- TODO: Update blank fragment layout -->
<SearchView
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:id="@+id/searchView"
    android:focusable="true"
    android:textStyle = "bold|italic"
    android:queryHint="@string/search"
    android:clickable="true"
    android:background="#ffffff" />

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="210dp"
    android:scrollbars="horizontal"
    android:id="@+id/horizontalScrollView">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/LoginFormContainer"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="300dp"
            android:layout_height="210dp"
            android:layout_marginRight="2dp"
            android:src="@drawable/picture1" />

        <ImageView
            android:layout_width="300dp"
            android:layout_height="210dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:src="@drawable/picture2" />

        <ImageView
            android:layout_width="300dp"
            android:layout_height="210dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:src="@drawable/picture3" />

        <ImageView
            android:layout_width="300dp"
            android:layout_height="210dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:src="@drawable/picture6" />

        <ImageView
            android:layout_width="300dp"
            android:layout_height="210dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:src="@drawable/hospital" />

        <ImageView
            android:layout_width="300dp"
            android:layout_height="210dp"
            android:layout_marginLeft="2dp"
            android:src="@drawable/picture7" />

    </LinearLayout>
</HorizontalScrollView>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cities"
    android:id="@+id/cities"
    android:textAppearance="?android:textAppearanceSmall"
    android:textStyle="bold"
    android:textColor="#ff0000"
    android:layout_marginTop="5dp"/>



<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ScrollView">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    style="@style/Container">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:layout_marginLeft="7dp"
    android:text="@string/bengaluru"
    android:gravity="center"
    android:textStyle="bold"
    android:textColor="#ffffff"
    android:textAppearance="?android:textAppearanceMedium"
    android:id="@+id/bengaluru"
    android:background="@drawable/bengaluru"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:text="@string/chennai"
    android:layout_marginLeft="7dp"
    android:layout_marginTop="2dp"
    android:id="@+id/chennai"
    android:textStyle="bold"
    android:textColor="#ffffff"
    android:textAppearance="?android:textAppearanceMedium"
    android:background="@drawable/chennai"
    android:gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:text="@string/kolkata"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="2dp"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:textAppearance="?android:textAppearanceMedium"
        android:gravity="center"
        android:id="@+id/kolkata"
        android:background="@drawable/kolkata"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:text="@string/mumbai"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="2dp"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:textAppearance="?android:textAppearanceMedium"
        android:gravity="center"
        android:id="@+id/mumbai"
        android:background="@drawable/mumbai"/>


        </LinearLayout>
    </ScrollView>


</LinearLayout>

2 个答案:

答案 0 :(得分:0)

将ScrolView设为父级,有助于垂直向内滚动,第一个元素将是您的水平滚动视图,这将使您的视图滚动水平

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

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="320px" android:layout_height="fill_parent">
             //your Contents here

    </HorizontalScrollView>

</ScrollView>

答案 1 :(得分:0)

不建议这样做。实际上,它建议不要这样做,并且不支持开箱即用。你能做的就是以下几点:

parentScrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
        findViewById(R.id.child_scroll).getParent()
                .requestDisallowInterceptTouchEvent(false);
        return false;
    }
});

childScrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});

但它确实不推荐。如果你尝试这样做,事情会很快得到。想象一下,内部的scrollview占据了小型设备上的所有屏幕,然后滚动外部滚动屏幕。