六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 34|回复: 0

Android屏幕大小,翻转,控件居中相关技巧

[复制链接]

升级  30%

25

主题

25

主题

25

主题

秀才

Rank: 2

积分
95
 楼主| 发表于 2013-2-3 14:37:56 | 显示全部楼层 |阅读模式
读取屏的方向:

public void onCreate() {if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {Log.i("info", "landscape");} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {Log.i("info", "portrait");}}
  
如果在androidmanifest.xml中加入配置

android:configChanges="orientation|keyboardHidden|navigation
当屏幕翻转时,Activity就不会重复的调用onCreate()、onPause()和onResume().

而是调用onConfigurationChanged(Configuration newConfig)

获取屏的大小:
int screenWidth, screenHeight;WindowManager windowManager = getWindowManager();Display display = windowManager.getDefaultDisplay();screenWidth = display.getWidth();screenHeight = display.getHeight();int screenWidth, screenHeight;WindowManager windowManager = getWindowManager();Display display = windowManager.getDefaultDisplay();screenWidth = display.getWidth();screenHeight = display.getHeight();// 不显示标题requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置窗口全屏显示getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 取得屏幕信息DisplayMetrics dm = new DisplayMetrics();dm = this.getResources().getDisplayMetrics();// 获得屏幕宽度int screenWidth = dm.widthPixels;// 获得屏幕高度int screenHeight = dm.heightPixels;

控件居中:

android:layout_centerInParent="true"

文本对齐方式:

android:gravity="right"
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表