본문 바로가기

Android/ScrollView

Android ScrollView example

ScrollView, HorizontalScrollView example

scroll_demo.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </ScrollView> </HorizontalScrollView> </LinearLayout>


ScrollDemo.java

package com.example.androidapp; import android.app.Activity; import android.content.res.*; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.ViewGroup; import android.widget.*; public class ScrollDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.scroll_demo);

ImageView iv = (ImageView) findViewById(R.id.imageView1);

ScrollView sv = (ScrollView) findViewById(R.id.scrollView1); sv.setHorizontalScrollBarEnabled(true); Resources res = getResources(); BitmapDrawable bitmap = (BitmapDrawable) res. getDrawable(R.drawable.mountain2); int imgW = bitmap.getIntrinsicWidth(); int imgH = bitmap.getIntrinsicHeight(); iv.setImageDrawable(bitmap); ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) iv.getLayoutParams(); params.width = imgW; params.height = imgH; } }