我正在我的应用程序中进行一些测试,以便将它们带到其他应用程序中,但我在实现启动屏幕 api 时遇到问题。相反,我遇到的问题是,一旦我将设备设置为横向模式,屏幕上的结果就会出现。设备的开头或结尾会出现一个白色“框”。这发生在模拟器中,就像在物理设备中一样。
*编辑我在另一个没有广告的应用程序中使用了相同的 api,并且得到了相同的结果。
主要活动
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initializeComponent()
buttonBanner.setOnClickListener {
AdsManager(this).showBanner(bannerLayout, this)
}
buttonInter.setOnClickListener {
AdsManager(this).showInterstitial(this)
}
buttonReward.setOnClickListener {
AdsManager(this).showReward(this)
}
}
Xml 启动
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/teal_700</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
<item name="postSplashScreenTheme">@style/Theme.UnityAds</item>
</style>
清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".UnityAds"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.App.Starting" >
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.App.Starting"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Xml main_activity
<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="@color/teal_700"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:text="Show Banner"
app:layout_constraintBottom_toTopOf="@+id/buttonInter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/buttonInter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:text="Show Interstitial"
app:layout_constraintBottom_toTopOf="@+id/buttonReward"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonBanner" />
<Button
android:id="@+id/buttonReward"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:text="Show Reward"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonInter" />
<include
android:id="@+id/bannerLayout"
layout="@layout/banner_layout"
android:layout_width="320dp"
android:layout_height="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/buttonReward" />
</androidx.constraintlayout.widget.ConstraintLayout>
横幅布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_horizontal"
android:layout_width="320dp"
android:layout_height="50dp">
主题
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.UnityAds" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- 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:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
我刚刚遇到了同样的问题。问题在于闪屏 API 弄乱了窗口背景颜色。无论您在何处定义实际的应用程序主题(在启动屏幕后调用),您都需要定义窗口背景颜色。那部分具体就是那个区域。
例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Start" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/splashscreen_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/splashscreen_logo</item>
<item name="postSplashScreenTheme">@style/Theme.YourAppTheme</item>
</style>
<style name="Theme.YourAppTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:statusBarColor">@color/black</item>
<!-- Set this to fix the white box on the left in landscape -->
<item name="android:windowBackground">@color/black</item>
</style>
</resources>