使用 ScrollView 在一个屏幕中显示多个图像视图
Mutiple Imageviews in one Screen with a ScrollView
我想在我的首页上有几个 ImageView's
,那是我的主要 class,比如六个,两列排列,我想这样做是因为我想成为能够实现简单的点击事件监听器。然而,由于 ScrollView
总是想要一个 child,我不知道如何添加滚动视图。我目前正在使用 LinearLayout
垂直方向。
这是我想用 ScrollView
实现的示例
到目前为止我的代码:)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".Activities.MainActivity"
tools:showIn="@layout/app_bar_main"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:paddingTop="60dp">
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image1"
android:scaleType="fitXY"
app:srcCompat="@drawable/events_gradient"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image2"
android:scaleType="fitXY"
app:srcCompat="@drawable/kaa_rada"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp">
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:id="@+id/image3"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/doc"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image4"
android:scaleType="fitXY"
app:srcCompat="@drawable/mythgradient"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
Enclose your inner layout inside Scrollview like,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".Activities.MainActivity"
tools:showIn="@layout/app_bar_main"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:paddingTop="60dp">
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image1"
android:scaleType="fitXY"
app:srcCompat="@drawable/events_gradient"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image2"
android:scaleType="fitXY"
app:srcCompat="@drawable/kaa_rada"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp">
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:id="@+id/image3"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/doc"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image4"
android:scaleType="fitXY"
app:srcCompat="@drawable/mythgradient"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
我想在我的首页上有几个 ImageView's
,那是我的主要 class,比如六个,两列排列,我想这样做是因为我想成为能够实现简单的点击事件监听器。然而,由于 ScrollView
总是想要一个 child,我不知道如何添加滚动视图。我目前正在使用 LinearLayout
垂直方向。
这是我想用 ScrollView
实现的示例
到目前为止我的代码:)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".Activities.MainActivity"
tools:showIn="@layout/app_bar_main"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:paddingTop="60dp">
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image1"
android:scaleType="fitXY"
app:srcCompat="@drawable/events_gradient"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image2"
android:scaleType="fitXY"
app:srcCompat="@drawable/kaa_rada"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp">
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:id="@+id/image3"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/doc"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image4"
android:scaleType="fitXY"
app:srcCompat="@drawable/mythgradient"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
Enclose your inner layout inside Scrollview like,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".Activities.MainActivity"
tools:showIn="@layout/app_bar_main"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:paddingTop="60dp">
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image1"
android:scaleType="fitXY"
app:srcCompat="@drawable/events_gradient"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image2"
android:scaleType="fitXY"
app:srcCompat="@drawable/kaa_rada"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp">
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:id="@+id/image3"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/doc"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="182dp"
android:layout_height="245dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image4"
android:scaleType="fitXY"
app:srcCompat="@drawable/mythgradient"
android:contentDescription="@string/todo" />
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>