Android橫豎屏幕切換小結(jié)
Android手機(jī)或平板都會(huì)存在橫豎屏切換的功能,通常是由物理重力感應(yīng)觸發(fā)的,但是有時(shí)候也不盡然,通常在設(shè)置里面我們可以對(duì)手機(jī)的橫豎屏切換進(jìn)行關(guān)閉。
AndroidManifest.xml
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yanlei.yl7" > <!-- Include required permissions for Google Mobile Ads to run. --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- This meta-data tag is required to use Google Play Services. --> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Include the AdActivity configChanges and theme. --> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> </application> </manifest>
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" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獲取Configuration信息" android:textSize="25sp" android:layout_marginTop="80dip" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測(cè)試ConfigurationChange" android:textSize="25sp" android:layout_centerInParent="true" android:id="@+id/mytext" /> </RelativeLayout>
MainActivity.java
package com.example.yanlei.yl7; import android.content.res.Configuration; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button mButton; private TextView pTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pTextView=(TextView)this.findViewById(R.id.mytext); System.out.println("---> onCreate()"); init(); } private void init() { mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(new ClickListenerImpl()); } private class ClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { getConfigurationInfo(); } } private void getConfigurationInfo() { Configuration configuration = getResources().getConfiguration(); //獲取屏幕方向 int l = configuration.ORIENTATION_LANDSCAPE; int p = configuration.ORIENTATION_PORTRAIT; if (configuration.orientation == l) { pTextView.setText("現(xiàn)在是橫屏===="); System.out.println("現(xiàn)在是橫屏"); } if (configuration.orientation == p) { pTextView.setText("現(xiàn)在是豎屏==="); System.out.println("現(xiàn)在是豎屏"); } } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); //newConfig.orientation獲得當(dāng)前屏幕狀態(tài)是橫向或者豎向 //Configuration.ORIENTATION_PORTRAIT 表示豎向 //Configuration.ORIENTATION_LANDSCAPE 表示橫屏 if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){ pTextView.setText("現(xiàn)在是豎屏"); Toast.makeText(MainActivity.this, "現(xiàn)在是豎屏", Toast.LENGTH_SHORT).show(); } if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){ pTextView.setText("現(xiàn)在是橫屏"); Toast.makeText(MainActivity.this, "現(xiàn)在是橫屏", Toast.LENGTH_SHORT).show(); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("name", "zxx"); outState.putInt("id", 9527); System.out.println("---> onSaveInstanceState()"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); String name = savedInstanceState.getString("name"); int id = savedInstanceState.getInt("id"); System.out.println("---> onRestoreInstanceState()"); System.out.println("名字=" + name + ",編號(hào)=" + id); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
以上內(nèi)容給大家介紹了Android橫豎屏切換小結(jié)的相關(guān)知識(shí),希望對(duì)大家有所幫助!
- android橫豎屏切換不重啟activity解決方案
- android橫豎屏切換時(shí)候Activity的生命周期
- android實(shí)現(xiàn)在橫豎屏切換時(shí)頁(yè)面信息不被重置的示例分享
- Android Activity 橫豎屏切換的生命周期
- 解析Android橫豎屏切換的問(wèn)題
- Android編程實(shí)現(xiàn)橫豎屏切換時(shí)不銷毀當(dāng)前activity和鎖定屏幕的方法
- android中Activity橫豎屏切換的那些事
- 解決Android橫豎屏切換數(shù)據(jù)丟失問(wèn)題的方法
- Android實(shí)現(xiàn)橫豎屏切換的實(shí)例代碼
- Android橫豎屏切換及其對(duì)應(yīng)布局加載問(wèn)題詳解
相關(guān)文章
Android 限制edittext 整數(shù)和小數(shù)位數(shù) 過(guò)濾器(詳解)
下面小編就為大家?guī)?lái)一篇Android 限制edittext 整數(shù)和小數(shù)位數(shù) 過(guò)濾器(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Android ListView實(shí)現(xiàn)圖文列表顯示
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)圖文列表顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的實(shí)例代碼
進(jìn)度條在Android中教程使用到,本文章向大家介紹一下Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,需要的朋友可以參考一下。2016-11-11Android反編譯看看手Q口令紅包的實(shí)現(xiàn)原理
這篇文章主要介紹了Android反編譯看看手Q口令紅包的實(shí)現(xiàn)原理,需要的朋友可以參考下2016-02-02Android中使用Handler及Countdowntimer實(shí)現(xiàn)包含倒計(jì)時(shí)的閃屏頁(yè)面
這篇文章主要介紹了Android中使用Handler及Countdowntimer實(shí)現(xiàn)包含倒計(jì)時(shí)的閃屏頁(yè)面,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03詳解Android studio 3+版本apk安裝失敗問(wèn)題
這篇文章主要介紹了詳解Android studio 3+版本apk安裝失敗問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Android實(shí)現(xiàn)打開手機(jī)淘寶并自動(dòng)識(shí)別淘寶口令彈出商品信息功能
最近項(xiàng)目經(jīng)理給我們安排一個(gè)活兒,基于Android開發(fā)實(shí)現(xiàn)打開手機(jī)淘寶,并自動(dòng)識(shí)別淘口令,彈出商品信息,今天小編就抽空給大家分享下這個(gè)需求是怎么實(shí)現(xiàn)的,需要的朋友參考下吧2017-11-11Android實(shí)現(xiàn)listview動(dòng)態(tài)加載數(shù)據(jù)分頁(yè)的兩種方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)listview動(dòng)態(tài)加載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06