如何动态获取屏幕方向

我们知道 android在屏幕旋转以后,有些情况会销毁当前的Activity. 也就是说从新启动个Activity. 然后 跑onCreate()  onStart()等流程.

还有中情况是 屏幕旋转后,不从新启动这个Activity. 只做些动作.

第一中情况是正常情况的流程. 第二中情况是你在manifest.xml里注册了android:config后的动作,你告诉了喜欢,当哪写配置改变后会做什么.

那如何动态检测当前屏幕方向及大小呢?


public class orientation extends Activity {
    TextView textview=null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textview=(TextView)findViewById(R.id.textview);
        int screenwidth=getWindowManager().getDefaultDisplay().getWidth();
             int screenheight=getWindochanger codewManager().getDefaultDisplay().getHeight();
             Log.d("orient", "now the screen width is :"+screenwidth+"screen height is:"+screenheight);

        int orient=getRequestedOrientation();
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            Log.d("orient", "now the screen orient is landscape");
        }
        else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {
        Log.d("orient", "now the screen orient is portrait");
        }
    }
}

哎,这样就很明显了,问题的根源是配置改变,那就找配置信息.getResources().getConfiguration即可获得当前配置对象.

这样读就可以了.

当然getWindowManager().getDefaultDisplay().getHeight() getWidth()就获得了高度,宽度.获得值会随屏幕方向改变而改变.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值