美文网首页UI
activity全屏显示且不影响状态栏和导航栏的实现

activity全屏显示且不影响状态栏和导航栏的实现

作者: 王灵 | 来源:发表于2021-07-07 14:40 被阅读0次

    先上效果图,
    有导航栏时


    Screenshot_20210707_141532_com.matthew.demo.jpg

    没有导航栏时


    Screenshot_20210707_141723_com.matthew.demo.jpg

    需要解决的问题

    • 全屏显示
    • 状态栏显示在view的上面
    • 如果有标题栏需要距离顶部状态栏的高度
      -当有导航栏时需要把导航栏的高度空出来

    实现

    1、定义一个activity需要的theme

        <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowTranslucentStatus">true</item>
            <item name="android:windowTranslucentNavigation">false</item>
        </style>
    

    2、给activity设置上

            <activity android:name=".MainActivity"
                android:theme="@style/MyTheme">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    3、写一个获取状态栏高度的方法

    fun getStatusBarHeight(context: Context): Int {
        //需要在Activity中执行
        var height = 0
        var resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            height = context.resources.getDimensionPixelSize(resourceId)
        }
        return height;
    }
    

    4、给标题栏设置距上的距离
    我的xml时这样的

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/mn001"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:id="@+id/ll_back"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#ffffff"
            android:gravity="center_vertical"
            android:paddingLeft="15dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <TextView
    
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Back"
                android:textSize="18dp"
                android:textStyle="bold" />
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    给整个layout设置了一个美女做背景。然后用一个LinearLayout实现标题栏,android:id="@+id/ll_back"

    在activity里设置ll_back的上边距即可

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContentView(R.layout.activity_main)
    
            var layoutParams = ll_back.layoutParams as ConstraintLayout.LayoutParams
            layoutParams.setMargins(0, getStatusBarHeight(this), 0, 0)
        }
    }
    
    fun getStatusBarHeight(context: Context): Int {
        //需要在Activity中执行
        var height = 0
        var resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            height = context.resources.getDimensionPixelSize(resourceId)
        }
        return height;
    }
    

    bradle

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    android {
        compileSdkVersion 29
        buildToolsVersion "30.0.2"
    
        defaultConfig {
            applicationId "com.matthew.demo"
            minSdkVersion 21
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: "libs", include: ["*.jar"])
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        implementation 'androidx.core:core-ktx:1.1.0'
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    
    }
    

    相关文章

      网友评论

        本文标题:activity全屏显示且不影响状态栏和导航栏的实现

        本文链接:https://www.haomeiwen.com/subject/tfboultx.html