編輯:關於Android編程
Android手機或平板都會存在橫豎屏切換的功能,通常是由物理重力感應觸發的,但是有時候也不盡然,通常在設置裡面我們可以對手機的橫豎屏切換進行關閉。
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="測試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("現在是橫屏===="); System.out.println("現在是橫屏"); } if (configuration.orientation == p) { pTextView.setText("現在是豎屏==="); System.out.println("現在是豎屏"); } } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); //newConfig.orientation獲得當前屏幕狀態是橫向或者豎向 //Configuration.ORIENTATION_PORTRAIT 表示豎向 //Configuration.ORIENTATION_LANDSCAPE 表示橫屏 if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){ pTextView.setText("現在是豎屏"); Toast.makeText(MainActivity.this, "現在是豎屏", Toast.LENGTH_SHORT).show(); } if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){ pTextView.setText("現在是橫屏"); Toast.makeText(MainActivity.this, "現在是橫屏", 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 + ",編號=" + 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); } }
以上內容給大家介紹了Android橫豎屏切換小結的相關知識,希望對大家有所幫助!
在學習Android開發的過程你,你往往會去借鑒別人的應用是怎麼開發的,那些漂亮的動畫和精致的布局可能會讓你愛不釋手,作為一個開發者,你可能會很想知道這些效果界面是怎麼去
前言:前面一篇分析了mediaplayerservice及MediaPlayer中的CS模型,但是對於如何能把數據解析出來,渲染到最終的SurfaceView上顯示,並且
1、Service的種類 按運行地點分類: 類別 區別 優點 缺點 應用 本地服務(Local) 該服務依附在主進程上, 服務依附在主進程上而不是獨立的進
1. 默認為debug mode,使用的簽名文件在: $HOME/.android/debug.keystore 2. Release Mode 簽