Android实现布局全屏

本文实例为大家分享了Android实现布局全屏的具体代码,供大家参考,具体内容如下

前言

类似Launcher,希望占用的布局铺满全屏,以调整状态栏及虚拟按键部分的颜色样式。

废话不多说,上案例:

一、效果预览

二、案例实现

1.新建Android工程
2.styles样式增加

values 目录的styles.xml添加如下样式:

<style name="FullTheme" parent="@style/BaseFullTheme">
</style>
<style name="BaseFullTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:colorBackgroundCacheHint">@null</item>
  <item name="android:windowShowWallpaper">true</item>
  <item name="android:windowNoTitle">true</item>
</style>

alues-v19 目录的styles.xml添加如下样式:

<style name="FullTheme" parent="@style/BaseFullTheme">
  <item name="android:windowTranslucentStatus">true</item>
  <item name="android:windowTranslucentNavigation">true</item>
</style>

values-v21目录的styles.xml添加如下样式:

<style name="FullTheme" parent="@style/BaseFullTheme">
  <item name="android:windowTranslucentStatus">false</item>
  <item name="android:windowTranslucentNavigation">false</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">#00000000</item>
  <item name="android:navigationBarColor">#00000000</item>
</style>

values-v29目录的styles.xml添加如下样式:

<style name="FullTheme" parent="@style/BaseFullTheme">
  <item name="android:colorBackgroundCacheHint">@null</item>
  <item name="android:colorEdgeEffect">#FF757575</item>
  <item name="android:windowActionBar">false</item>
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowShowWallpaper">true</item>
  <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
  <item name="android:enforceStatusBarContrast">false</item>
  <item name="android:enforceNavigationBarContrast">false</item>

  <item name="android:windowTranslucentStatus">false</item>
  <item name="android:windowTranslucentNavigation">false</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">#00000000</item>
  <item name="android:navigationBarColor">#00000000</item>
</style>

3.布局

layout目录建立activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/holo_blue_bright"<!-- 测试设置的颜色 -->
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试"
        >
    </Button>
</LinearLayout>

4.使用

新建MainActivity.java

package com.demo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideStatusBarNavigationBar();
        setContentView(R.layout.activity_main);
    }

   //关键方法
    private void hideStatusBarNavigationBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }
    }
}

AndroidManifest.xml声明

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/FullTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

finish

三、填坑:fitsSystemWindows之坑

在activity_main.xml中的根布局那增加了android:fitsSystemWindows=“true”,如果不增加这个属性,子view的布局会从最顶上开始,有兴趣的可以修改了试试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android中3种全屏方法及3种去掉标题栏的方法

    一.去掉标题栏的方法 第一种:入门的时候经常使用的一种方法 复制代码 代码如下: requestWindowFeature(Window.FEATURE_NO_TITLE);  //去掉标题栏注意这句一定要写在setContentView()方法的前面,不然会报错的 第二种:在AndroidManifest.xml文件中定义 复制代码 代码如下: <application android:icon="@drawable/icon" android:label="@str

  • android activity设置无标题实现全屏

    Activity设置全屏和无标题栏,要用到andorid.view.Window和Android.view.WindowManager. Window.FEATURE_NO_TITLE表示无标题栏. WindowManager.LayoutParams.FLAG_FULLSCREEN表示全屏. 具体用法如下: 1.设置全屏可以使用如下代码: getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager

  • android 设置全屏的两种方法

    现在android的每一个项目都会需要设置为全屏,现在介绍两种设置为全屏的方式. 一.在配置文件中设置android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 如: 二.在activity中设置 这两种方式都可以设置全屏,任选其一即可.

  • Android 全屏无标题栏的三种实现方法

    一.通过Java代码 在setContentView之前执行: requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏 二.调用Android自带的Theme 直接在AndroidManifest.xml中需要全屏显

  • Android下Activity全屏显示实现方法

    本文较为详细的讲述了Android下Activity全屏显示实现方法.分享给大家供大家参考.具体方法如下: 方法一: 使用xml的方法,在该项目的AndroidManifest.xml文件中,在需要全屏的Activity元素中添加属性: 复制代码 代码如下: android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 这样就可以实现这个Activity的全屏显示,如果只是不要标题栏,即需要保留系统自带的任务栏的话,则使用: 复制

  • Android 设置应用全屏的两种解决方法

    在开发中我们经常需要把我们的应用设置为全屏,有两种方法,一中是在代码中设置,另一种方法是在配置文件里改! 一.在代码中设置: 复制代码 代码如下: package com.android.tutor;  import android.app.Activity;  import android.os.Bundle;  import android.view.Window;  import android.view.WindowManager;  public class OpenGl_Lesson

  • Android开发之全屏与非全屏的切换设置方法小结

    本文实例讲述了Android开发之全屏与非全屏的切换设置方法.分享给大家供大家参考,具体如下: 静态方法 1. 代码方式 在Activity类OnCreate方法中设置,代码如下 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().se

  • Android4.2中全屏或者取消标题栏的方法总结

    先介绍去掉标题栏的方法: 第一种:也一般入门的时候经常使用的一种方法 requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏注意这句一定要写在setContentView()方法的前面,不然会报错的 第二种:在AndroidManifest.xml文件中定义 复制代码 代码如下: <application android:icon="@drawable/icon" android:label="@string/app_

  • Android编程实现WebView自适应全屏方法小结

    本文实例讲述了Android编程实现WebView自适应全屏的方法.分享给大家供大家参考,具体如下: 第一种: settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true); 第二种: WebSetting settings = webView.getSettings(); settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 把所有内容放在we

  • Android编程实现WebView全屏播放的方法(附源码)

    本文实例讲述了Android编程实现WebView全屏播放的方法.分享给大家供大家参考,具体如下: 最近因为项目要用webview加载html5的视频,开始不能全屏播,做了很久才做出来!那按我的理解说下怎么实现全屏吧. 首先写布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.

随机推荐