Pages

Thứ Ba, 18 tháng 6, 2019

Xoay màn hình

Ví dụ xoay màn hình trong Android

screenOrientation  là thuộc tính của phần tử activity. Hướng màn hình của một activity có thể là: dọc(portrait), landscape, sensor, không xác định .v.v, . Cần phải khai báo nó trong tập tin. ví dụ:

  <activity  android:name="com.example.screenorientation.MainActivity"                android:label="@string/app_name"                 android:screenOrientation="landscape" />  

Các giá trị chung cho thuộc tính screenOrientation như sau:

Giá trị Mô tả
unspecified Giá trị mặc định. không xác định
portrait Theo chiều dọc
landscape Theo chiều ngang
sensor orientation sẽ dò hướng bằng cách dựa vào sensor

Ví dụ chế độ màn hình theo hướng landscape trong Android

File: activity_main.xml

  <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:paddingBottom="@dimen/activity_vertical_margin"        android:paddingLeft="@dimen/activity_horizontal_margin"        android:paddingRight="@dimen/activity_horizontal_margin"        android:paddingTop="@dimen/activity_vertical_margin"        tools:context=".MainActivity" >            <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="66dp"            android:layout_marginTop="73dp"            android:text="Button"            android:onClick="onClick"             />            <EditText            android:id="@+id/editText1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerHorizontal="true"            android:ems="10" />        </RelativeLayout>  

Activity class

File: MainActivity.java

  package com.example.f;        import android.os.Bundle;    import android.app.Activity;    import android.view.Menu;    import android.view.View;    import android.view.View.OnClickListener;    import android.widget.Button;    import android.widget.EditText;        public class MainActivity extends Activity{        EditText editText1;        Button button1;        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);                        editText1=(EditText)findViewById(R.id.editText1);            button1=(Button)findViewById(R.id.button1);        }        public void onClick(View v) {            editText1.setText("O android");        }    }  

AndroidManifest.xml

File: AndroidManifest.xml


Download ví dụ


Kết quả:

 



Cập nhật công nghệ từ Youtube tại link: https://www.youtube.com/channel/UCOxeYcvZPGf-mGLYSl_1LuA/videos
Để tham gia khóa học công nghệ truy cập link: http://thuvien.hocviendaotao.com
Mọi hỗ trợ về công nghệ email: dinhanhtuan68@gmail.com