作者:江苏科技大学机械工程学院 赵长金
1.在应用中固定屏幕方向。
在AndroidManifest.xml的activity中加入:
android:screenOrientation=”landscape”
属性即可(landscape是横向,portrait是纵向)。

2.随屏幕旋转时,不重新调用onCreate。
当将手机屏幕旋转时,系统会被强制重置启动onCreate方法。

1)修改AndroidManifest.xml

在activity属性中加入:
android:configChanges=”orientation|keyboardHidden”
android:configChanges,这个方法主要是负责列出清单,当清单上用户指定的设置改变时,Activity会自己处理这些变化。
orientation,屏幕界面旋转(可能是用户手动旋转的),【注意:如果你的开发API等级等于或高于13,你还需要设置screenSize,因为screenSize会在屏幕旋转时改变】
keyboardHidden,键盘辅助功能改变
2)在相对应的Activity中继承重写onConfigurationChanged方法,这个方法将会在我们的应用发生变化时,让我们能随心所谓地进行监听处理。
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {

} else {

}
}
作者:江苏科技大学机械工程学院 赵长金
还有界面设计方面的问题,Android手机大部分是HVGA、WVGA的分辨率,屏幕视觉上比较“狭长”。往往竖着看很合适的布局,当屏幕横向翻转以后 显示会变得很别扭。当屏幕由竖直方向改变为横向时,我们可以把界面中的控件由本来的垂直线性布局修改为横向线性布局,这样布局会更合理一些。我们可以自己 写一个布局类集成LinearLayout布局,通过覆盖onMeasure方法来实现这种自动布局。当屏幕的宽高发生改变时,系统会调用 onMeasure方法。通过这个方法,我们可以获得改变以后的宽高尺寸,从而来实现屏幕翻转的自动布局,主要代码如下:

Java代码
/**
* 屏幕改变时自动调用
* @param widthMeasureSpec 改变后的宽度
* @param heightMeasureSpec 改变后的高度
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
/*宽度*/
int screenWith = View.MeasureSpec.getSize(widthMeasureSpec);
/*高度*/
int screenHeight = View.MeasureSpec.getSize(heightMeasureSpec);

/*竖直布局*/
if (screenWith < screenHeight)
{

this.setOrientation(VERTICAL);
for (int i = 0; i < getChildCount(); i++)
{
View childView = getChildAt(i);
if (childView instanceof CakyCanvas)
{
/*该控件占布局的2/5*/
LayoutParams params = new LayoutParams(screenWith,
screenHeight * 2/ 5
updateViewLayout(childView, params);
}
else if (childView instanceof CakyExplainCanvas)
{
/*该控件占布局的3/5*/
LayoutParams params = new LayoutParams(screenWith,
screenHeight * 3/ 5
updateViewLayout(childView, params);
}
}
}
/*横向布局*/
else
{

this.setOrientation(HORIZONTAL);
for (int i = 0; i < getChildCount(); i++)
{
View childView = getChildAt(i);
if (childView instanceof CakyCanvas)
{
LayoutParams params = new LayoutParams(
screenWith * 2/ 5
screenHeight);
updateViewLayout(childView, params);
}
else if (childView instanceof CakyExplainCanvas)
{
LayoutParams params = new LayoutParams(
screenWith * 3/ 5
screenHeight);
updateViewLayout(childView, params);
}
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

留笔,希望对大家有帮助!!!

作者:江苏科技大学机械工程学院 赵长金

更多相关文章

  1. activity在配置只支持竖屏时要注意个问题
  2. # android笔记 #
  3. Android应用借助LinearLayout实现垂直水平居中布局
  4. Android(安卓)ApiDemos示例解析(101):Views->Auto Complete->3.
  5. Android(安卓)frameworks去掉熄屏前先变暗的功能
  6. android:layout_marginEnd隐藏的坑 or setMargins无效
  7. 模仿墨迹天气设置界面布局
  8. Android获取屏幕或View宽度和高度的方法
  9. Android学习中一些小项目(连载中)

随机推荐

  1. 如何把C++的源程序改写成C语言
  2. PHP中的国际化日历类
  3. 聊聊springcloud项目同时存在多个注册中
  4. 开发中总结的dart相关的技巧
  5. 如何实现一个iOS AOP框架?
  6. 怎么在日常工作中践行逻辑思维能力?
  7. 永远不要在代码中使用「User」这个单词!
  8. tsconfig.json文件各字段吐血整理
  9. PHP中针对区域语言标记信息的操作
  10. Caffeine缓存的简单介绍