无法在 SDK AP1 21 中使状态栏透明:Android 5.0 (lollipop)
Unable to make the Status Bar transparent in SDK AP1 21: Android 5.0 (lollipop)
想使状态栏透明,但它变黑了而不是透明的。怎么了 ?所有方法都用过了,但还没有 changing.I 将父主题从 MaterialComponents 更改为 App Compat ,是因为那个吗?
这是我的 themes.xml 文件。
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.QuizApp" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:windowFullscreen">true</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="fontFamily">@font/font10</item>
</style>
<style name="heading_name" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="fontFamily">@font/font1</item>
<item name="textFillColor">#2B0031</item>
</style>
<style name="EditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/purple_gray</item>
<item name="colorControlActivated">@color/purple_gray</item>
<item name="colorControlHighlight">@color/purple_gray</item>
<item name="hintTextColor">@color/purple_gray</item>
<item name="android:textColorHint">@color/purple_gray</item>
<item name="boxStrokeErrorColor">@color/purple_gray</item>
</style>
<style name="Theme.QuizApp.NoActionBarAndStatusBar" >
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:fitsSystemWindows">true</item>
<item name="fontFamily">@font/font10</item>
</style>
<style name="progressBarBlue" parent="@style/Theme.AppCompat">
<item name="colorAccent">#4568dc</item>
</style>
<style name="Theme.QuizApp.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.QuizApp.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
这是 activity_main.xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/screen_background"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
style="@style/heading_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:fontFamily="@font/font1"
android:gravity="center"
android:shadowColor="#450750"
android:shadowDx="1.5"
android:shadowDy="1.5"
android:shadowRadius="1.6"
android:text="Quiz App !"
android:textColor="@color/white"
android:textColorHighlight="#8F22A1"
android:textColorHint="#7F1491"
android:textSize="55sp"
android:textStyle="bold" />
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
app:cardElevation="50dp"
app:cardMaxElevation="100dp"
app:cardUseCompatPadding="false"
android:layout_marginEnd="20dp"
app:cardCornerRadius="12dp"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/font3"
android:gravity="center_horizontal"
android:text="Welcome ! "
android:textColor="#000000"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Enter Your Name "
android:textStyle="bold"
android:textSize="20sp"
android:gravity="center_horizontal"
android:layout_marginTop="16dp"
android:textColor="@color/black"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
android:layout_marginTop="20dp">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
tools:text="Nandini"
android:textCursorDrawable="@drawable/cursor"
android:inputType="textCapWords"
android:textColor="#000000"
android:shadowColor="@color/purple_gray"
android:theme="@style/EditTextTheme"
android:textColorHighlight="@color/purple_gray"
android:textColorLink="@color/purple_gray"
android:backgroundTint="@color/purple_gray"
app:hintTextColor="@color/purple_gray"
android:outlineAmbientShadowColor="@color/purple_gray"
android:textColorHint="@color/purple_gray"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Start Quiz"
android:textStyle="bold"
android:elevation="100dp"
android:stateListAnimator="@null"
android:layout_gravity="center_horizontal"
android:background="@drawable/button_bg"
android:textColor="@color/white"
android:textSize="20sp"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
这是 MainActivity.kt 文件。
package com.nandini.android.quizapp
import android.content.Intent
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn_start : Button =findViewById(R.id.btn_start)
val et_name : EditText=findViewById(R.id.et_name)
btn_start.setOnClickListener {
if(et_name.text.isEmpty())
{
et_name.setError("")
Toast.makeText(this, "Enter name",Toast.LENGTH_LONG).show()
}else{
val intent = Intent(this,QuizQuestionActivity::class.java)
intent.putExtra(Constants.USER_NAME,et_name.text.toString())
startActivity(intent)
}
}
}
}
尝试在您的主题中添加这两行:
<item name="android:fitsSystemWindows">true</item>
<item name="android:statusBarColor" tools:targetApi="l">@android:color/transparent</item>
并且您可以毫无问题地使用 Theme.MaterialComponents.DayNight.NoActionBar
作为您的主题。
想使状态栏透明,但它变黑了而不是透明的。怎么了 ?所有方法都用过了,但还没有 changing.I 将父主题从 MaterialComponents 更改为 App Compat ,是因为那个吗?
这是我的 themes.xml 文件。
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.QuizApp" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:windowFullscreen">true</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="fontFamily">@font/font10</item>
</style>
<style name="heading_name" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="fontFamily">@font/font1</item>
<item name="textFillColor">#2B0031</item>
</style>
<style name="EditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/purple_gray</item>
<item name="colorControlActivated">@color/purple_gray</item>
<item name="colorControlHighlight">@color/purple_gray</item>
<item name="hintTextColor">@color/purple_gray</item>
<item name="android:textColorHint">@color/purple_gray</item>
<item name="boxStrokeErrorColor">@color/purple_gray</item>
</style>
<style name="Theme.QuizApp.NoActionBarAndStatusBar" >
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:fitsSystemWindows">true</item>
<item name="fontFamily">@font/font10</item>
</style>
<style name="progressBarBlue" parent="@style/Theme.AppCompat">
<item name="colorAccent">#4568dc</item>
</style>
<style name="Theme.QuizApp.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.QuizApp.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
这是 activity_main.xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/screen_background"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
style="@style/heading_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:fontFamily="@font/font1"
android:gravity="center"
android:shadowColor="#450750"
android:shadowDx="1.5"
android:shadowDy="1.5"
android:shadowRadius="1.6"
android:text="Quiz App !"
android:textColor="@color/white"
android:textColorHighlight="#8F22A1"
android:textColorHint="#7F1491"
android:textSize="55sp"
android:textStyle="bold" />
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
app:cardElevation="50dp"
app:cardMaxElevation="100dp"
app:cardUseCompatPadding="false"
android:layout_marginEnd="20dp"
app:cardCornerRadius="12dp"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/font3"
android:gravity="center_horizontal"
android:text="Welcome ! "
android:textColor="#000000"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Enter Your Name "
android:textStyle="bold"
android:textSize="20sp"
android:gravity="center_horizontal"
android:layout_marginTop="16dp"
android:textColor="@color/black"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
android:layout_marginTop="20dp">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
tools:text="Nandini"
android:textCursorDrawable="@drawable/cursor"
android:inputType="textCapWords"
android:textColor="#000000"
android:shadowColor="@color/purple_gray"
android:theme="@style/EditTextTheme"
android:textColorHighlight="@color/purple_gray"
android:textColorLink="@color/purple_gray"
android:backgroundTint="@color/purple_gray"
app:hintTextColor="@color/purple_gray"
android:outlineAmbientShadowColor="@color/purple_gray"
android:textColorHint="@color/purple_gray"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Start Quiz"
android:textStyle="bold"
android:elevation="100dp"
android:stateListAnimator="@null"
android:layout_gravity="center_horizontal"
android:background="@drawable/button_bg"
android:textColor="@color/white"
android:textSize="20sp"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
这是 MainActivity.kt 文件。
package com.nandini.android.quizapp
import android.content.Intent
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn_start : Button =findViewById(R.id.btn_start)
val et_name : EditText=findViewById(R.id.et_name)
btn_start.setOnClickListener {
if(et_name.text.isEmpty())
{
et_name.setError("")
Toast.makeText(this, "Enter name",Toast.LENGTH_LONG).show()
}else{
val intent = Intent(this,QuizQuestionActivity::class.java)
intent.putExtra(Constants.USER_NAME,et_name.text.toString())
startActivity(intent)
}
}
}
}
尝试在您的主题中添加这两行:
<item name="android:fitsSystemWindows">true</item>
<item name="android:statusBarColor" tools:targetApi="l">@android:color/transparent</item>
并且您可以毫无问题地使用 Theme.MaterialComponents.DayNight.NoActionBar
作为您的主题。