在android中,判断横屏还是竖屏,并且根据方向改变背景,代码如下:
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);
}
}
其中Background_v是纵向时的背景图,view.setBackgroundResource为横向时的背景图
然后在activity的oncreate方法中去调用
LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
//背景自动适应
androidUtil.AutoBackground(this, layout, R.drawable.bg_v, R.drawable.bg_h);
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);
}
}
其中Background_v是纵向时的背景图,view.setBackgroundResource为横向时的背景图
然后在activity的oncreate方法中去调用
LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
//背景自动适应
androidUtil.AutoBackground(this, layout, R.drawable.bg_v, R.drawable.bg_h);