最近在做一个android 的平板项目,属于二次开发,这个项目原来的布局界面都是横向写死,现在的需要是适应横竖屏2个方向。
所以我的方法是,onCreate()方法时,通过获取屏幕宽高,如果发现是竖屏,需要重新把原来的横屏布局用代码形式重新布局,如果是横屏,使用默认的布局不用修改
int width=DensityUtil.getWindowWidth();//这里使用了一个自己写的工具类
int height=DensityUtil.getWindowHeight();
if(height>width){
//说明是竖屏,根据需要改变里面控件的布局
}//否则是横屏,不改栋
/** 获取屏幕宽度 */
@SuppressWarnings("deprecation")
public static int getWindowWidth() {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getWidth();
}
/** 获取屏幕高度 */
@SuppressWarnings("deprecation")
public static int getWindowHeight() {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisp