一、设置横竖屏

设置横竖屏的时候在切换的时候会导致activity重启,即销毁创建回到初始状态,为了让窗体不重启前面的博客里也讲到了:在activity的属性里加上:android:configChanges="orientation|keyboardHidden",这里指定了虚拟键盘和横竖屏切换不会重启activity。(android api的升级现在必须改成):

android:configChanges="orientation|keyboardHidden|screenSize"

  (1)代码中实现:横屏是ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,竖屏对应的值是:ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

判断是否为横屏
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
//设置横屏
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 }

  (2)清单文件里设置:设置在application里时候是对所有的activity,在单个的activity里时对这个activity有效

竖屏
android:screenOrientation="portrait"
横屏 android:screenOrientation="landscape"

 二、设置全屏

同样2中方法。

  (1)代码中设置,在activity的 setContentView(R.layout.activity_main);加载前去掉标题栏和信息栏:

 以前可以这样:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_main);

    }

  但是为了兼容以前版本,继承AppCompatActivity后这样设置还是会有状状态栏:

解决办法:在清单文件配置主题:

“@style/Theme.AppCompat.NoActionBar”

然后oncreate里调用下面的去掉状态栏即可
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏

 

 (2)在清淡文件里设置android:theme="@android:style/Theme.NoTitleBar.Fullscreen",这里运行就异常的原因是继承了ActionBarActivity,而没有继承activity,导致activity里的相关主题不能用到,因此修改继承Activity就好了

以前的方法:

 <activity
            android:name="com.example.mycamera.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
             >

 现在这么配置会报错,兼容新的版本后Activity里的一些主题不能用了。

解决办法:自己可以在style文件里写自己的主题:建议直接看后面的简单方法

麻烦但是可以编写自己的主题的方法:重写主题的方式可以自己定义buttonbar等一些格式,在values目录下创建attrs.xml文件来声明一些自己定义的控件或其他的格式,申明后可以在style里使用。下面给出一种声明和在style里定义自己的 无标题主题、无标题无状态栏主题

当主题中color没有时可自己在color.xml中添加。

metaButtonBarStyle 和 metaButtonBarButtonStyle 的定义在attrs.xml中

1、attrs.xml

<resources>

    <!-- Declare custom theme attributes that allow changing which styles are
         used for button bars depending on the API level.
         ?android:attr/buttonBarStyle is new as of API 11 so this is
         necessary to support previous API levels. -->
    <declare-styleable name="ButtonBarContainerTheme">
        <attr name="metaButtonBarStyle" format="reference" />
        <attr name="metaButtonBarButtonStyle" format="reference" />
    </declare-styleable>

</resources>
View Code

相关文章: