QMUI是腾讯针对Android系统开发的一套UI界面插件,功能丰富强大,重点是原生Android开源,现就将QMUI在Android Studio 3.5.2下的配置方法以及如何实现沉浸式顶部的方法讲解如下。
首先配置QMUI使用环境,build.gradle(Module)配置如下,注意粗体标注部分:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.gsgwyks"
minSdkVersion 19 //最低API版本19以上
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-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.qmuiteam:qmui:2.0.0-alpha02'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
def qmui_arch_version = '2.0.0-alpha02'
implementation "com.qmuiteam:arch:$qmui_arch_version"
kapt "com.qmuiteam:arch-compiler:$qmui_arch_version"
}
最后将样式表style.xml 修改如下就可以正常使用QMUI界面了
下面我们通过QMUI实现沉浸式顶部,首先修改布局文件如下
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
通过QMUIWindowInsetLayout 内嵌 QMUITopBarLayout实现
然后在脚本修改如下
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\_main)
QMUIStatusBarHelper.translucent(this)
topbar.setBackgroundColor(ContextCompat.getColor(applicationContext,R.color.qmui\_config\_color\_blue))
topbar.setTitle("沉浸式顶部测试成功").setTextColor(ContextCompat.getColor(applicationContext,R.color.qmui\_config\_color\_50\_white))
}
注意针对 kotlin访问控件的写法要引入
import kotlinx.android.synthetic.main.activity_main.*
然后编译效果如下