在做android项目中,同事碰到个音乐播放器,结果界面是需要横竖屏切换的,当时只做了个竖屏
的,结果横屏是出错了,所以我就自己查了下,总结了如何操作横竖屏,及其显示.
(把antivity的一些方法里加入log信息)
首先我们先来了解下横竖屏切换时Activity走的流程:
1.没有任何处理,就拿普普通通的HelloWorld的例子:运行HelloWorld. 默认竖屏走的流程
是onCreate->onStart->onResume
然后我们按ctrl+f11 切换成横屏走的流程是 onSaveInstanceState-->onPause-->onStop--
>onDestroy-->onCreate-->onStart-->onRestoreInstanceState-->onResume
从中我们可以发现是重新走了次onCreate
我们再把横屏切换成竖屏,发现它把竖屏变横屏的流程走了两遍..这是不是对我们界面的
变化更改带来很大的麻烦...
2.我们在AndroidManifest.xml 中写上这么一句话
android:configChanges="orientation"在运行次看看,发现log信息会多出来一个 onConfigurationChanged,加了比原来还多走了一步,= =~!!其实要再多加一个信息就行啦~~修改成android:configChanges="orientation|keyboardHidden"这时最后运行次程序,发现了么!!在切换横竖屏时,log信息只有一条.onConfigurationChanged(横屏切换成竖屏时还是会走两次),这样我们就能在这里实现横竖屏显示的区别了..下面就是代码了
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yijun"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LandActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="2"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="3"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="4"/>
<Button
android:id="@+id/b5"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="5"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/b5"
android:layout_alignTop="@+id/b5"
android:text="6"/>
<!-- android:layout_centerVertical="true" 这个替换alignTop也是可以的 -->
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/b5"
android:layout_alignTop="@+id/b5"
android:text="7"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/b5"
android:layout_alignLeft="@+id/b5"
android:text="8"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/b5"
android:layout_alignLeft="@+id/b5"
android:text="9"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/b5"
android:text="10"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/b5"
android:text="11"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/b5"
android:layout_alignParentRight="true"
android:text="12"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/b5"
android:layout_alignParentBottom="true"
android:text="13"/>
</RelativeLayout>

简要的概述下如何横竖屏切换时显示个字的view,一般切换界面常用的两个方法,一个通过Activity的跳转,另一个是加载不同的view.我这里是通过三种方法加载view显示的:1.最简单的布局时用RelativeLayout布局,这样不管横竖屏都能按要求显示出来,2.通过LayoutInflater,加载xml布局文件,3.动态布局
LandActivity.java
package com.yijun;
import android.R.raw;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.location.GpsStatus.Listener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
*
* @author yijun.wu android项目中时常会出现横竖屏两种界面,所以有时候我们需要特别处理
* 这里用了三种方法来显示当横屏的时候显示的view,第一种在布局的时候就用相对布局,那样横竖屏都会自动布局
* 第二种使用LayoutInflater,第三种就是动态布局
*/
public class LandActivity extends Activity {
private static final String TAG = "LandActivity";
private int mOrientation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
// 获取手机横竖屏
mOrientation = this.getResources().getConfiguration().orientation;
// 横竖屏加载不同的布局
if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.main);
} else {
setContentView(R.layout.a);
}
}
// android:configChanges="orientation|keyboardHidden"设置后只走这里
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged");
// 判断横竖屏
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// 第一种简单直接
// setContentView(R.layout.a);
/*************************************************************/
// 第二种通过LayoutInflater
// LayoutInflater inflate = getLayoutInflater();
// View view = inflate.inflate(R.layout.a, null);
// TextView t1 = (TextView) view.findViewById(R.id.t1);
// TextView t2 = (TextView) view.findViewById(R.id.t2);
// Button b1 = (Button) view.findViewById(R.id.b1);
// t1.setText("tttt");
// t2.setText("lalallala");
// t2.setTextColor(Color.RED);
// t2.setBackgroundColor(Color.WHITE);
// b1.setText("I am a button");
// setContentView(view);
/**********************************************************/
// 第三种动态布局
// 定义线性布局管理器
LinearLayout layout = new LinearLayout(this);
// 定义布局管理器的指定宽和高
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
// 设置线性布局的方向
layout.setOrientation(LinearLayout.VERTICAL);
// 定义要显示组件的布局管理器
LinearLayout.LayoutParams txtParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams buttonParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
Button button = new Button(this);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.a);
}
});
// 显示的文字
textView.setText("动态设置布局增加的TextView组件");
button.setText("点击按钮");
// 设置文本按钮的参数
textView.setLayoutParams(txtParam);
button.setLayoutParams(buttonParam);
// 增加组件
layout.addView(button, buttonParam);
layout.addView(textView, txtParam);
// 增加新的布局管理器
super.setContentView(layout, params);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.main);
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
Log.i(TAG, "onWindowFocusChanged");
}
}
希望大家多给意见哈
