Android实现布局全屏

作者:xiaowen2008821 时间:2023-11-29 06:44:13 

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

前言

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

废话不多说,上案例:

一、效果预览

Android实现布局全屏

二、案例实现

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的布局会从最顶上开始,有兴趣的可以修改了试试。

来源:https://blog.csdn.net/xiaowen2008821/article/details/116454786

标签:Android,布局,全屏
0
投稿

猜你喜欢

  • Springboot如何通过yml配置文件为静态成员变量赋值

    2021-08-03 12:03:17
  • MyBatis-Plus逻辑删除和字段自动填充的实现

    2023-01-08 03:45:54
  • C#中把英文字母转换为大写或小写的方法

    2021-06-13 18:46:55
  • 详解Java弱引用(WeakReference)的理解与使用

    2022-07-13 12:37:32
  • Java数据结构之链表详解

    2023-10-20 09:14:08
  • java组件commons-fileupload实现文件上传

    2022-10-02 08:33:14
  • Redis使用RedisTemplate模板类的常用操作方式

    2023-01-30 08:33:40
  • 单例模式 分析代码优化方法

    2021-07-28 15:49:51
  • Android 三级NestedScroll嵌套滚动实践

    2022-11-12 07:45:21
  • c#扩展datatable转json示例

    2022-08-19 10:44:55
  • WPF仿三星手机充电界面实现代码

    2021-09-16 06:20:44
  • Java实现角色扮演游戏的示例代码

    2023-03-31 19:41:45
  • WinForm下 TextBox只允许输入数字的小例子

    2023-08-02 15:34:14
  • Android利用Intent.ACTION_SEND进行分享

    2023-07-10 05:02:18
  • Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解

    2022-11-28 10:03:09
  • Java设计模式之java原型模式详解

    2022-04-01 03:16:40
  • 在Android模拟器上模拟GPS功能总是null的解决方法

    2023-09-16 16:51:15
  • 深入浅出探索Java分布式锁原理

    2021-12-16 11:58:07
  • SpringMVC入门实例

    2023-02-04 12:43:48
  • C#将Excel中的数据转换成DataSet

    2021-10-29 18:20:55
  • asp之家 软件编程 m.aspxhome.com