这篇文章给大家分享的是有关Android实现布局全屏的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Android是什么Android是一种基于linux内核的自由及开放源代码的操作系统,主要使用于移动设备,
这篇文章给大家分享的是有关Android实现布局全屏的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
Android是一种基于linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
前言
类似Launcher,希望占用的布局铺满全屏,以调整状态栏及虚拟按键部分的颜色样式。
上案例:
新建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>
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>
新建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
在activity_main.xml中的根布局那增加了android:fitsSystemWindows=“true”,如果不增加这个属性,子view的布局会从最顶上开始,有兴趣的可以修改了试试。
感谢各位的阅读!关于“Android实现布局全屏的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
--结束END--
本文标题: Android实现布局全屏的方法
本文链接: https://www.lsjlt.com/news/275981.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0