android中判断横屏或者竖屏并改变背景
16lz
2022-05-15
今天学到一招,在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);
更多相关文章
- android背景选择器selector用法汇总
- android中判断横屏或者竖屏并改变背景
- Android(安卓)activity属性设置大全
- Android中取消GridView & ListView默认的点击背景色
- Android开发:布局分区域设置不同背景色
- android Button 点击背景与文字颜色变化效果
- android中判断横屏或者竖屏并改变背景
- android中判断横屏或者竖屏并改变背景
- android 用 XML 自定义边框(只上下边框有色)