【摘录】横竖屏切换
http://www.cnblogs.com/hll2008/archive/2011/01/03/1924952.html
public?static?int?ScreenOrient(Activity?activity)
????{
????????int?orient?=?activity.getRequestedOrientation();?
????????if(orient?!=?ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE?&&?orient?!=?ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)?{
????????????//寬>高為橫屏,反正為豎屏??
?????????????WindowManager?windowManager?=?activity.getWindowManager();??
?????????????Display?display?=?windowManager.getDefaultDisplay();??
?????????????int?screenWidth??=?display.getWidth();??
?????????????int?screenHeight?=?display.getHeight();??
?????????????orient?=?screenWidth?<?screenHeight???ActivityInfo.SCREEN_ORIENTATION_PORTRAIT?:?ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
????????}
????????return?orient;
????}
????{
????????int?orient=ScreenOrient(activity);
????????if?(orient?==?ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)?{?//縱向?
????????????view.setBackgroundResource(Background_v);
????????}else{?//橫向
????????????view.setBackgroundResource(Background_h);
????????}??
????}
//背景自動適應
AndroidHelper.AutoBackground(this,?layout,?R.drawable.bg_v,?R.drawable.bg_h);
為了實現橫豎屏切換顯示,背景圖的顯示采用代碼進行控制顯示,首先用如下方法獲取當前手機是橫屏還是豎屏:
代碼//獲取屏幕方向public?static?int?ScreenOrient(Activity?activity)
????{
????????int?orient?=?activity.getRequestedOrientation();?
????????if(orient?!=?ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE?&&?orient?!=?ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)?{
????????????//寬>高為橫屏,反正為豎屏??
?????????????WindowManager?windowManager?=?activity.getWindowManager();??
?????????????Display?display?=?windowManager.getDefaultDisplay();??
?????????????int?screenWidth??=?display.getWidth();??
?????????????int?screenHeight?=?display.getHeight();??
?????????????orient?=?screenWidth?<?screenHeight???ActivityInfo.SCREEN_ORIENTATION_PORTRAIT?:?ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
????????}
????????return?orient;
????}
?
然后編寫一個名為AutoBackground的公共方法用來實現屏幕背景的自動切換,后面的幾乎每一個功能頁面都需要用到這個方法:
代碼public?static?void?AutoBackground(Activity?activity,View?view,int?Background_v,?int?Background_h)????{
????????int?orient=ScreenOrient(activity);
????????if?(orient?==?ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)?{?//縱向?
????????????view.setBackgroundResource(Background_v);
????????}else{?//橫向
????????????view.setBackgroundResource(Background_h);
????????}??
????}
?
完成上述兩方法后在?MainActivity的onCreate方法中調用AutoBackground方法進行屏幕自動切換:
LinearLayout?layout=(LinearLayout)findViewById(R.id.layout);//背景自動適應
AndroidHelper.AutoBackground(this,?layout,?R.drawable.bg_v,?R.drawable.bg_h);
?
到此完成了載入頁面的UI部分的實現,測試運行模擬器中查看效果,基本上跟最上面的設計效果圖相符,測試效果圖如下:
?
轉載于:https://www.cnblogs.com/IamEasy_Man/archive/2011/08/09/2132069.html
總結
- 上一篇: 数论基础_素数相关
- 下一篇: 简单明了 - Git 使用超详细教程