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();
}
}
摘要“
动态改变控件宽高
<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;
//获取设置的配置信息
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;
相关文章推荐
- android设置横竖屏、可触控、获取分辨率等代码
- Android - xml动画,识别手势动作,代码抽取,获取手机SIM卡串号,获取联系人数据,开机广播,发送/解析短信,报警音乐
- Android 代码片段---获取手机通讯录列表
- Android获取cpu和内存信息、网址的代码
- Android获取基站坐标代码
- Android客户端从服务器端获取json数据并解析的实现代码
- android单元测试获取toast代码
- android手机获取gps和基站的经纬度地址实现代码
- ANDROID设备获取IMEI,手机号码等常用代码片段
- android 代码中获取Resource中的字符串
- android两行代码获取内外置sd卡路径
- android代码中获取view的宽高方式
- android典型代码系列(二十四)------获取某个应用程序所占用的内存
- Android编程获取网址HTML代码的方法
- android 内核代码的获取,编译和使用
- Android 获取Root权限之后的静默安装实现 代码示例分析
- Android使用phonegap从相册里面获取照片(代码分享)
- Android 最简洁的获取联系人头像的代码
- Android拍照获取照片路径并上传至服务器+Servlet代码
- android git和repo的使用,获取android代码