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

HorizontalScrollView - Android Widgets Example

HorizontalScrollView - Android Widgets สำหรับ HorizontalScrollView เป็น Widget ที่ใช้สำหรับการแสดงผล HorizontalScrollView หรือ Scrollbar ในตำแหน่งของแนวนอน (Horizontal) ส่วนถ้าจะใช้ในแนวตั้งให้ใช้ ScrollView

HorizontalScrollView - Android Widgets

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

Example 1 การใช้ HorizontalScrollView เพื่อสร้าง Scrollbar ในแนวนอน (Horizontal) แบบง่าย ๆ

HorizontalScrollView - Android Widgets

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

activity_main.xml (XML Layout)
<HorizontalScrollView 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="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    	<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" />
        </LinearLayout>
        
    	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:orientation="vertical">
	          <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" />
	     </LinearLayout>
	     
    	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:orientation="vertical">
	          <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" />
		</LinearLayout>
		
    	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:orientation="vertical">
	          <TextView
	            android:id="@+id/textView4"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="Picture 3" />  	     
		     <ImageView
		        android:id="@+id/imageView4"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:src="@drawable/pic_d" />
	     </LinearLayout>	
	        
    </LinearLayout>
</HorizontalScrollView>


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

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

HorizontalScrollView - Android Widgets

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





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


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.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends Activity {

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

      
        HorizontalScrollView hsv = new HorizontalScrollView(this);
        
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.HORIZONTAL);
		hsv.addView(ll);
		
		// Column 1
		LinearLayout ll1 = new LinearLayout(this);
		ll1.setOrientation(LinearLayout.VERTICAL);
		ll.addView(ll1);
		// Picture 1 & pic_a
		TextView tv1 = new TextView(this);
		tv1.setText("Picture 1");
		tv1.setGravity(Gravity.CENTER_VERTICAL);
		ll1.addView(tv1);
		ImageView iv1 = new ImageView(this);
		iv1.setImageResource(R.drawable.pic_a);
		ll1.addView(iv1);

		
		// Column 2
		LinearLayout ll2 = new LinearLayout(this);
		ll2.setOrientation(LinearLayout.VERTICAL);
		ll.addView(ll2);
		// Picture 2 & pic_b
		TextView tv2 = new TextView(this);
		tv2.setText("Picture 2");
		tv2.setGravity(Gravity.CENTER_VERTICAL);
		ll2.addView(tv2);
		ImageView iv2 = new ImageView(this);
		iv2.setImageResource(R.drawable.pic_b);
		ll2.addView(iv2);
		
		// Column 3
		LinearLayout ll3 = new LinearLayout(this);
		ll3.setOrientation(LinearLayout.VERTICAL);
		ll.addView(ll3);		
		// Picture 3 & pic_c
		TextView tv3 = new TextView(this);
		tv3.setText("Picture 3");
		tv3.setGravity(Gravity.CENTER_VERTICAL);
		ll3.addView(tv3);
		ImageView iv3 = new ImageView(this);
		iv3.setImageResource(R.drawable.pic_c);
		ll3.addView(iv3);
		
		// Column 4
		LinearLayout ll4 = new LinearLayout(this);
		ll4.setOrientation(LinearLayout.VERTICAL);
		ll.addView(ll4);			
		// Picture 4 & pic_d
		TextView tv4 = new TextView(this);
		tv4.setText("Picture 4");
		tv4.setGravity(Gravity.CENTER_VERTICAL);
		ll4.addView(tv4);
		ImageView iv4 = new ImageView(this);
		iv4.setImageResource(R.drawable.pic_d);
		ll4.addView(iv4);
		
		this.setContentView(hsv);

    }

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


Screenshot

HorizontalScrollView - Android Widgets

แสดง HorizontalScrollView หรือ Scrollbar ในแนวนอน (Horizontal)




Example 3 การใช้ HorizontalScrollView และ ScrollView เพื่อแสดง Scrollbar ในแนวตั้งและแนวนอนพร้อมกัน

activity_main.xml (XML Layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
	<TextView
	  android:layout_width="fill_parent"
	  android:layout_height="wrap_content"
	  android:text="Test ScrollView and HorizontalScrollView "/>
	
		<HorizontalScrollView
		  android:layout_width="wrap_content"
		  android:layout_height="wrap_content">
		  
			<LinearLayout
			  android:orientation="horizontal"
			  android:layout_width="wrap_content"
			  android:layout_height="wrap_content">
				
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 1" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 2" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 3" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 4" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 5" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 6" />
			    
			</LinearLayout>
			
		</HorizontalScrollView>

		<ScrollView
		android:layout_height="fill_parent"
		android:layout_width="fill_parent">
			<LinearLayout
			  android:orientation="vertical"
			  android:layout_width="wrap_content"
			  android:layout_height="wrap_content" >

			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 1" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 2" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 3" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 4" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 5" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 6" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 7" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 8" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 9" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 10" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 11" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 12" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 13" />
			    <Button
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
			        android:text="Button 14" />
			    
			</LinearLayout>
		</ScrollView>
</LinearLayout>


จาก XML Layout ข้างต้นมีการแบ่งการแสดงผล HorizontalScrollView และ ScrollView เพื่อจะแยกการแสดงผลของ Scrollbar ในแนวตั้ง และ แนวนอน

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

HorizontalScrollView - Android Widgets

จากตัวอย่างแสดง Scrollbar ทั้งในแนวตั้ง และ แนวนอน






   
Share

Property & Method (Others Related)

Android Composite Widgets
ListView - Android Widgets Example
GridView - Android Widgets Example
ScrollView - 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:45 / 2012-07-14 07:58:30
  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 01
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 อัตราราคา คลิกที่นี่