[置顶] android中横竖屏幕的处理

 支持横竖屏幕的切换代码如下:     

 

  public class TestActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout layout=(LinearLayout)findViewById(R.id.test_linearlayout); AutoBackground(this,layout,R.drawable.portrait,R.drawable.landscape); } 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; } 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); } } } 
   

你可能感兴趣的:(android,layout,Class)