Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

ScrollView - Android Widgets Example

ScrollView - Android Widgets สำหรับ ScrollView เป็น Widget ที่ใช้สำหรับการแสดงผล ScrollView หรือ Scrollbarที่อยู่ในตำแหน่งของแนวตั้ง โดยถ้าใช้ ScrollView ครอบ Widgets อื่น ๆ และข้อมูลใน Form มียาวหรือเยอะลงไปก็จะสามารถใช้ ScrollView เพื่อเลื่อนดูข้อมูลอื่น ๆ ได้

ScrollView - Android Widgets

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

Example 1 การใช้ ScrollView เพื่อสร้าง Scrollbarแบบง่าย ๆ

ScrollView - Android Widgets

ออกแบบหน้าจอ GraphicalLayout ด้วย Widget ตามรูป

activity_main.xml (XML Layout)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Picture 1" />
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pic_a" />
        
          <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Picture 2" />      
	     <ImageView
	        android:id="@+id/imageView2"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic_b" />
	     
          <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Picture 3" />   
	     <ImageView
	        android:id="@+id/imageView3"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic_c" />
	     
          <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Picture 4" />  	     
	     <ImageView
	        android:id="@+id/imageView4"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic_d" />	
	        
    </LinearLayout>
</ScrollView>


จาก XML Layout ข้างต้นจะใช้การแสดงรูปภาพผ่าน ImageView หลาย ๆ ตัว เพื่อจะทดสอบการทำงานของ ScrollView

MainActivity.java (Java Code)
package com.myapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
}


Screenshot

ScrollView - Android Widgets

จากตัวอย่างจะมีรูปภาพอยู่หลายรายการ จึงทำให้เกิด Scrollbar ขึ้น ซึ่งจะสามารถเลื่อนขึ้นลง เพื่อดูข้อมูลอื่น ๆ





Example 2 การสร้าง LinearLayout และ Widgets ของ Image แบบ Dynamic Runtime และการแสดงผล ScrollView


MainActivity.java (Java Code)
package com.myapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

      
		ScrollView sv = new ScrollView(this);
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		sv.addView(ll);
		

		
		// Picture 1 & pic_a
		TextView tv1 = new TextView(this);
		tv1.setText("Picture 1");
		tv1.setGravity(Gravity.CENTER_VERTICAL);
		ll.addView(tv1);
		ImageView iv1 = new ImageView(this);
		iv1.setImageResource(R.drawable.pic_a);
		ll.addView(iv1);

		
		// Picture 2 & pic_b
		TextView tv2 = new TextView(this);
		tv2.setText("Picture 2");
		tv2.setGravity(Gravity.CENTER_VERTICAL);
		ll.addView(tv2);
		ImageView iv2 = new ImageView(this);
		iv2.setImageResource(R.drawable.pic_b);
		ll.addView(iv2);
		
		// Picture 3 & pic_c
		TextView tv3 = new TextView(this);
		tv3.setText("Picture 3");
		tv3.setGravity(Gravity.CENTER_VERTICAL);
		ll.addView(tv3);
		ImageView iv3 = new ImageView(this);
		iv3.setImageResource(R.drawable.pic_c);
		ll.addView(iv3);
		
		// Picture 4 & pic_d
		TextView tv4 = new TextView(this);
		tv4.setText("Picture 4");
		tv4.setGravity(Gravity.CENTER_VERTICAL);
		ll.addView(tv4);
		ImageView iv4 = new ImageView(this);
		iv4.setImageResource(R.drawable.pic_d);
		ll.addView(iv4);
		
		this.setContentView(sv);

    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
}


Screenshot

ScrollView - Android Widgets

แสดง ScrollView หรือ Scrollbar






   
Share

Property & Method (Others Related)

Android Composite Widgets
ListView - Android Widgets Example
GridView - Android Widgets Example
HorizontalScrollView - Android Widgets Example
SearchView - Android Widgets Example
SlidingDrawer - Android Widgets Example
TabWidget - Android Widgets Example
TabHost - Android Widgets Example
WebView - Android Widgets Example

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-01 16:15:29 / 2012-07-14 07:51:47
  Download : No files
 Sponsored Links / Related

 
Android Custom Adapter
Rating :

 
Android People Contact List, Name, Phone No, Photo Picture, Email and Address
Rating :

 
Android Rating (Vote) and ListView Part 1
Rating :

 
Android Rating (Vote) and ListView Part 2 (Member Login and Average Rating)
Rating :

 
Android PhoneGap (jQuery Mobile) Create Convert App from Website(URL)
Rating :

 
Android Capture Image and Camera Capture Screenshot (android.view.SurfaceView)
Rating :

 
Android Pull Down to Refresh And Release to Refresh or Update (Part 1)
Rating :

 
Android Pull Down to Refresh And Release to Update (Part 2 , PHP & MySQL)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่