zoukankan      html  css  js  c++  java
  • Android 横竖屏总结


    横竖屏切换后Activity会重新执行onCreat函数,但是在Android工程的Mainfest.xml中加入
    android:screenOrientation="user" android:configChanges="orientation|keyboardHidden"之后,
    横竖屏切换之后就不会去执行OnCreat函数了,而是会去调用onConfigurationChanged(),这样我们就能控制横竖屏的切换了。
    或者在res目录下建立layout-land和layout-port目录,相应的layout文件不变。layout-land是横屏的layout,layout-port是竖屏的layout。


    如果想让它一直是横屏显示的话,只要在配置文件中设置android:screenOrientation="landscape"就行了,
    或者在代码中 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);。
    如果想让它一直是竖屏显示的话,只要在配置文件中设置android:screenOrientation="portrait"就行了,
    或者在代码中setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);。

     

    启动的时候是横屏的话就横屏表示,纵屏的话就纵屏表示,手机切换横竖屏不能用。
    第一步:在Mainfest.xml中追加android:screenOrientation="sensor" android:configChanges="orientation|keyboardHidden"

    第二步:取得屏幕的长和宽,进行比较设置横竖屏的变量。

    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    if (width > height) {
    orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    } else {
    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }


    第三步:在onConfigurationChanged()函数中追加this.setRequestedOrientation(mOrientation)就行了

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    this.setRequestedOrientation(mOrientation);
    }


    切到别的画面的时候再回到原画面,它就仍然是横的或者是纵的。想让它从别的屏幕回来后,又重新横竖屏布局,
    只要在OnResume()中在设定下就行了,但是这个只支持横竖屏只有一个layout的。

  • 相关阅读:
    May LeetCoding Challenge22 之 比较器comparator、map按Value排成逆序、桶排序
    May LeetCoding Challenge21 之 动态规划的min使用
    May LeetCoding Challenge20 之 二叉树中序遍历
    May LeetCoding Challenge19 之 单调栈2.0
    May LeetCoding Challenge18 之 滑动窗口2.0
    May LeetCoding Challenge17 之 滑动窗口
    May LeetCoding Challenge16 之 链表重组
    APT常用命令
    DDCTF-misc-流量分析
    Wireshark学习笔记
  • 原文地址:https://www.cnblogs.com/rfheh/p/4164832.html
Copyright © 2011-2022 走看看