您的位置:首页 > 移动开发 > Android开发

android代码获取横竖屏

2013-05-02 00:51 232 查看
第一种

//获取设置的配置信息

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: