Thursday, February 21, 2019

ScrollView


We use ScrollView to scroll a layout longer than screen height.
To scroll part of screen, we use this.
<ScrollView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:fillViewport="true" >
        <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical" >
    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@null"
        android:src="@drawable/abc" />
 <ImageView      
        android:layout_width="500dp"
        android:layout_height="230dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@null"
        android:src="@drawable/abc" />
 </LinearLayout>
  </ScrollView>
We must put all views in a LinearLayout with orientation vertical.
To scroll a whole screen, we wrap it in ScrollView like this.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
</ScrollView>
We use a LinearLayout vertical inside.
To horizontal scroll.
<HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fillViewport="true" >
</HorizontalScrollView>
If we want both vertical and horizontal scroll, use them like this.
<HorizontalScrollView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
  android:fillViewport="true" >
 <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:orientation="vertical" >  
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
 <LinearLayout
  android:layout_width="fill_parent"
 android:layout_height="wrap_content"
  android:orientation="vertical" >
  <ImageView
 android:id="@+id/img"
 android:layout_width="match_parent"
android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:contentDescription="@null"
 android:src="@drawable/abc" />
 <ImageView      
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:contentDescription="@null"
 android:src="@drawable/abc" />
 </LinearLayout>
 </ScrollView>
 </LinearLayout>
 </HorizontalScrollView>
If we want a TextView can scroll, put it inside a scrollview
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> 
<TextView
 android:id="@+id/tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:text=" Scroll TextView"
 android:textColor="#800000"
 android:textSize="26sp" />
</ScrollView>

No comments:

Post a Comment