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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
WebView 播放视频的全屏功能一般都是通过 Android 的横屏实现的,具体实现步骤如下: 1. 配置 AndroidManifest.xml 文件,启用横屏模式: ``` <activity android:name=".MainActivity" android:configChanges="orientation|screenSize" android:screenOrientation="sensorLandscape" android:theme="@style/AppTheme.NoActionBar.Fullscreen" /> ``` 其中,`android:configChanges="orientation|screenSize"` 表示屏幕旋转和尺寸变化时不重新创建 Activity,`android:screenOrientation="sensorLandscape"` 表示允许横向旋转屏幕,`android:theme="@style/AppTheme.NoActionBar.Fullscreen"` 表示使用全屏主题。 2. 在布局文件中添加 WebView: ``` <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. 在 Activity 中实现横屏逻辑: ``` class MainActivity : AppCompatActivity() { private lateinit var webView: WebView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE // 启用横屏模式 setContentView(R.layout.activity_main) webView = findViewById(R.id.webView) webView.settings.javaScriptEnabled = true webView.settings.pluginState = WebSettings.PluginState.ON webView.settings.allowFileAccess = true webView.webChromeClient = object : WebChromeClient() { override fun onShowCustomView(view: View?, callback: CustomViewCallback?) { super.onShowCustomView(view, callback) if (view is FrameLayout) { view.layoutParams = ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) setContentView(view) } } override fun onHideCustomView() { super.onHideCustomView() setContentView(webView) } } webView.loadUrl("https://www.example.com/video.html") } override fun onBackPressed() { if (webView.canGoBack()) { webView.goBack() } else { super.onBackPressed() } } override fun onDestroy() { super.onDestroy() webView.loadUrl("about:blank") webView.stopLoading() webView.webChromeClient = null webView.destroy() } } ``` 其中,onShowCustomView 方法会在 WebView 中播放视频时调用,onHideCustomView 方法会在视频播放结束后调用。在 onShowCustomView 方法中,我们将 WebView 的布局替换为全屏的视频布局,然后在 onHideCustomView 方法中将布局替换回来。 需要注意的是,在 onDestroy 方法中需要将 WebView 停止加载并销毁,以避免 WebView 内存泄漏的问题。 以上就是 Android WebView 播放视频全屏的实现方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaowen2008821

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值