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

Registered : 109,027

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > Android Rotate Switching Portrait and Landscape - Emulator (มุมมองในแนวตั้งและแนวนอน)



Clound SSD Virtual Server

Android Rotate Switching Portrait and Landscape - Emulator (มุมมองในแนวตั้งและแนวนอน)

Android Rotate Switching Portrait and Landscape - Emulator (มุมมองในแนวตั้งและแนวนอน) ในเครื่อง Smartphone ที่ติดตั้ง Android OS เราสามารถทำการหมุ่น Rotate เพื่อเปลี่ยนมุมมองของเครื่องให้อยู่ใน Portrait (แนวตั้ง) และ Landscape (แนวนอน) และหลาย ๆ App เราจะเห็นว่าเมื่อมีการเปลียนมุมมอง Switching ระหว่าง Portrait (แนวตั้ง) และ Landscape (แนวนอน) จะมีการทำงานของ Activity ที่ต่างกัน และ Layout ต่าง ๆ ก็จะเปลี่ยนไปเช่นเดียวกัน

Android Rotate Orientation การหมุ่นหน้าจอของ Emulator
Ctrl+Fn+F11= on Mac OS.
Ctrl+F11 = on Windows 7.
Ctrl+F11 = on Linux.


Android Rotate Switching Portrait and Landscape

Android Rotate Switching Portrait and Landscape


สำหรับบทความนี้เรามารู้วิธีการสลับ Switching มุมมองระหว่าง Portrait (แนวตั้ง) และ Landscape (แนวนอน) และการเขียนโปรแกรมเพื่อตรวจจับว่าในขณะนี้ได้อยู่ในมุมมองไหน เพื่อนำค่าที่ได้ไปเป็นเงื่อนไขต่าง ๆ

        int mode = getResources().getConfiguration().orientation;
        // 1 = Portrait
        // 2 = Landscape
        if (mode == 1) {
        	result.setText("Current Portrait");
        } else {
        	result.setText("Current Landscape");
        } 

จาก Code เราสามารถทำการอ่านสถานะของ Portrait and Landscape ได้ดง่าย ๆ ด้วยคำสั่ง getResources().getConfiguration().orientation; โดยค่า 1 คือ Portrait และ 2 คือ Landscape หรือจะเขียนอีกรูปแบบคือ

         int mode = getResources().getConfiguration().orientation;

        if (mode == Configuration.ORIENTATION_PORTRAIT) {
        	result.setText("Current Portrait");
         }else if (mode == Configuration.ORIENTATION_LANDSCAPE) {
        	result.setText("Current Landscape");
        } 

โดยเราสามารถเรียกค่าเหล่านี้ได้จาก method ของ onCreate()

Example ตัวอย่างการเขียน Android เพื่ออ่านสถานะของ Portrait (แนวตั้ง) และ Landscape (แนวนอน)

Android Rotate Switching Portrait and Landscape

โครงสร้างไฟล์ดังรูป

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="130dp"
        android:text="Result"
        android:gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>


Android Rotate Switching Portrait and Landscape

XML Layout ที่ได้








MainActivity.java
package com.myapp;

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

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final TextView result = (TextView) findViewById(R.id.txtResult);
        
        int mode = getResources().getConfiguration().orientation;

        if (mode == 1) {
        	result.setText("Current Portrait");
        } else {
        	result.setText("Current Landscape");
        } 
        
    }

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


Screenshot

Android Rotate Switching Portrait and Landscape

มุมมอง Portrait (แนวตั้ง)

Android Rotate Switching Portrait and Landscape

มุมมอง Landscape (แนวนอน)

   
Share


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


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


   


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

 
Android File Structure โครงสร้างของไฟล์ของ Android บน Smart Phone และ Tablets
Rating :

 
รู้จัก Palette หรือเครื่องมือ Widgets Element ต่าง ๆ ที่อยู่ใน Android และ Eclipse
Rating :

 
Android สร้าง Launcher Icons และ Title Bar บน Eclipse และ ใช้งาน Emulator เบื้องต้น
Rating :

 
พื้นฐานการเขียน Android กับโปรแกรมภาษา Java Syntax และการใช้งาน Eclipse เบื้องต้น
Rating :

 
ฟรี!! อบรม Android เรียน Android จะเขียน แอนดรอยด์ แหล่งความรู้แบบ ฟรี ๆ
Rating :

 
Android Change Launcher Icons การเปลี่ยนไอคอนของโปรแกรมทีเขียนบนแอนดรอยด์
Rating :

 
การแก้ปัญหาเบื้องต้นเมื่อ Emulator ไม่ตอบสนอง ไม่สามารถรันโปรแกรม Android ได้
Rating :

 
Android - Process system is not responding / has stopped / unexpectedly
Rating :

 
การใช้งาน Debugging (LogCat) ในการเขียน Android บนโปรแกรม Eclipse IDE (Android-Eclipse)
Rating :

 
การเซ็ตให้ Eclipse - Run / Debug โปรแกรม Android บน Device และ Smart Phone หรือ Tablets จริง
Rating :

 
Android Export APK การนำ App ที่เขียนบน Eclipse ทำเป็น .apk ไป Install ใช้งานจริงบน Smart Phone และ Tablets
Rating :

 
Android Sample Project ของ Android SDK ที่อยู่ใน Eclipse มีอะไรดีและน่าสนใจกว่าที่คิด
Rating :

 
เรียกใช้งาน string.xml บน android ไฟล์จัดเก็บค่า string / array ตัวแปรในรูปแบบต่าง ๆ
Rating :

 
Android กับ Intent ควบคุมการแสดงและซ่อน (Show/Hide) ของ Activity form ต่าง ๆ
Rating :

 
Android การใส่ Effect ให้กับ Intent ใน Activity ระหว่างการเปลี่ยน Activity
Rating :

 
Android กับ setContentView() ควบคุมการแสดงผล XML Layout ของแต่ล่ะ Activity
Rating :

 
Android Enabled/Disabled Fix Portrait or Landscape Orientation Mode
Rating :

 
Android Change Activity Layout when Switching Portrait and Landscape
Rating :

 
Android Display FullScreen / No Title Bar
Rating :

 
Android การทำ Splash Screen Loading ในช่วงระหว่างการเข้าสู่ App หรือระหว่างในช่วงรอการเปลี่ยนหน้า
Rating :

 
Android External Font Face / Font Color / Font Size
Rating :

 
Android Import Jar Library
Rating :

 
Android get current Date/Time
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 03
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 อัตราราคา คลิกที่นี่