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

Android and Custom SeekBar

Android and Custom SeekBar ในการสร้าง Custom SeekBar บน Android โดยปกติแล้ว SeekBar บน Android ในแต่ล่ะ Version ก็จะมีรูปแบบที่เป็นเอกลักษณ์อยู่แล้ว แต่ถ้าต้องการสร้าง Custom SeekBar ขึ้นมาเองก็สามารถทำได้ไม่ยาก โดยใช้การสร้าง Style ในรูปแแบบของ XML ส่วนวิธีการนั้นไม่ยาก ลองมาดูตัวอย่าง

Android and Custom SeekBar


Step การทำ Custom SeekBar

ในตัวอย่างนี้จะสร้าง Custom SeekBar ขึ้นมา 2 ตัวคือ
- CustomSeekBar1 (ไฟล์ที่เกี่ยวข้อง custom_seek_bar.xml และ string.xml )
- CustomSeekBar2 (ไฟล์ที่เกี่ยวข้อง custom_seek_bar.xml,custom_thumb_state_default.xml,custom_thumb_state_pressed.xml, custom_thumb_state_selected.xml และ string.xml )


โครงสร้างไฟล์บน Project

Android and Custom SeekBar

โครงสร้างของไฟล์ทั้งหมด จัดเก็บตาม Path และ Directory ดังภาพ

/res/drawable/custom_seek_bar.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list
	xmlns:android="http://schemas.android.com/apk/res/android">
	<item
		android:id="@android:id/background">
		<shape>
			<corners
				android:radius="10dip" />
			<gradient
				android:startColor="#ffffffff"
				android:centerColor="#ffdddddd"
				android:centerY="0.50"
				android:endColor="#ffffffff"
				android:angle="270" />
		</shape>
	</item>
	<item
		android:id="@android:id/secondaryProgress">
		<clip>
			<shape>
				<corners
					android:radius="10dip" />
				<gradient
					android:startColor="#77bf0d0d"
					android:endColor="#77ef5555"
					android:angle="90" />
			</shape>
		</clip>
	</item>
	<item
		android:id="@android:id/progress">
		<clip>
			<shape>
				<corners
					android:radius="10dip" />
				<gradient
					android:startColor="#ffbf0d0d"
					android:endColor="#ffef5555"
					android:angle="90" />
			</shape>
		</clip>
	</item>
</layer-list>









/res/drawable/custom_thumb_state_default.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="oval">
	<size
		android:width="35dip"
		android:height="35dip" />
	<stroke
		android:width="1dip"
		android:color="#ffffffff" />
	<gradient
		android:startColor="#ffcdcdcd"
		android:endColor="#fff8f8f8"
		android:angle="270"
		android:type="linear" />
</shape>


/res/drawable/custom_thumb_state_pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="oval">
	<size
		android:width="35dip"
		android:height="35dip" />
	<stroke
		android:width="1dip"
		android:color="#ff5da8d4" />
	<gradient
		android:startColor="#ff0e75af"
		android:endColor="#ff1997e1"
		android:angle="270"
		android:type="linear" />
</shape>


/res/drawable/custom_thumb_state_selected.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="oval">
	<size
		android:width="35dip"
		android:height="35dip" />
	<stroke
		android:width="1dip"
		android:color="#ffffc27e" />
	<gradient
		android:startColor="#ffed6100"
		android:endColor="#ffffc27e"
		android:angle="270"
		android:type="linear" />
</shape>



/values/string.xml
	 <style name="CustomSeekBar1" parent="android:Widget.SeekBar">
	   <item name="android:progressDrawable">@drawable/custom_seek_bar</item>
	   <item name="android:thumbOffset">0dip</item>
	</style>
  	
	<style name="CustomSeekBar2" parent="android:Widget.SeekBar">
	   <item name="android:progressDrawable">@drawable/custom_seek_bar</item>
	   <item name="android:thumb">@drawable/seek_bar_thumb</item>
	   <item name="android:thumbOffset">0dip</item>
	</style>

เพิ่มคำสั่งชุดนี้ลงใน string.xml


การเรียกใช้ Style บน Activity Layout

Android and Custom SeekBar

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
        
    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/CustomSeekBar1"/>
    
    <SeekBar
        android:id="@+id/seekBar3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/CustomSeekBar2"/>
 
</LinearLayout>


ใน Widgets ของ SeekBar จะมีการเรียกใช้ style="@style/CustomSeekBar1" และ style="@style/CustomSeekBar2"


MainActivity.java
package com.myapp;

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

public class MainActivity extends Activity {
	 
	 
 
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			
		}
}









Screenshot

Android and Custom SeekBar

แสดง Custom SeekBar ที่ได้สร้างขึ้นมา และการเปรียบเทียบกับ Default ของ SeekBar

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-08-30 17:01:52 / 2017-03-26 20:16:40
  Download : No files
 Sponsored Links / Related

 
Android กับ Dialog boxes การโต้ตอบเบื้องต้นกับ AlertDialog และ makeText(Toast)
Rating :

 
Android Dialogs(AlertDialog) and Popup
Rating :

 
Android Confirm [Yes/No] AlertDialog Popup
Rating :

 
Android SeekBar in Alert Dialog Popup
Rating :

 
Android RatingBar in AlertDialog Popup
Rating :

 
Android Toast Notifications Custom Display XML Layout
Rating :

 
Android and Custom RatingBar
Rating :

 
Android and Custom ProgressBar
Rating :

 
Android Popup Custom Layout and Returning (EditText/Textbox) from Dialog
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 อัตราราคา คลิกที่นี่