android代码获取横竖屏
第一种
//获取设置的配置信息
int ori = this.getResources().getConfiguration().orientation; //获取屏幕方向
if(ori == Configuration.ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Toast.makeText(this, 11+
"横屏", 0).show();
}else if(ori == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, 11+
"竖屏", 0).show();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
第二种
DisplayMetrics dm=this.getApplicationContext().getResources().getDisplayMetrics();
if (dm.widthPixels > dm.heightPixels) { // 横屏
Toast.makeText(this, "横屏", 0).show();
} else { // 竖屏
Toast.makeText(this, "竖屏", 0).show();
}
}
摘要“
android:screenOrientation="landscape" 永远强制横屏或竖屏,此时onConfigurationChanged(Configuration newConfig)不再响应
动态改变控件宽高
<RelativeLayout android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/flow_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_menu_moreoverflow_normal_holo_dark"
android:scaleType="center"
android:layout_above="@+id/v"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
flowMmenu = (ImageView) findViewById(R.id.flow_menu);
LayoutParams layoutParams = (LayoutParams) flowMmenu.getLayoutParams();
layoutParams.height = 1;
flowMmenu.setLayoutParams(layoutParams);
注:LayoutParams应用的包名:import android.widget.RelativeLayout.LayoutParams;