[英]Runtime overlays android with appcompat
I want to make runtime overlays for an application that uses appcompat but I can't get it to work.我想为使用 appcompat 的应用程序制作运行时覆盖,但我无法让它工作。
I followed this Example我按照这个例子
First: I created and android application that i want to overlay.第一:我创建了我想要覆盖的 android 应用程序。 I'm trying to overlay the string in textview "Hello World" to be "Hello World overlay!"
我正在尝试将文本视图“Hello World”中的字符串覆盖为“Hello World overlay!”
The App code snaps应用程序代码快照
Main activity:主要活动:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Main activity layout:主要活动布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
</FrameLayout>
Manifest:显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/MyTheme"
android:supportsRtl="true">
<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>
App theme:应用主题:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
String resources字符串资源
<string name="hello_world">Hello World!</string>
Till this point the code is working fine:)到目前为止,代码工作正常:)
Then I create the overlay project that include the overlay resource.然后我创建包含覆盖资源的覆盖项目。
The Overlay project code snaps: Overlay项目代码截图:
Manifest:显现:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.overlay">
<overlay
android:priority="1"
android:targetPackage="com.example.app"/>
<application android:hasCode="false"/>
</manifest>
The string resources that i want to overlay them in runtime我想在运行时覆盖它们的字符串资源
<string name="hello_world">Hello World Overlay!</string>
I built and installed the apk.And i can find it as overlay when i'm using adb shell cmd overlay list
我构建并安装了 apk。当我使用
adb shell cmd overlay list
时,我可以找到它作为覆盖
Once I enabled the overlay using adb shell cmd overlay enable --user 0 com.example.overlay
I got this crash.一旦我使用
adb shell cmd overlay enable --user 0 com.example.overlay
,我就遇到了这个崩溃。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:57)
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:5238)
at android.app.ActivityThread.access$3400(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2026)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:552)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.app.MainActivity.onCreate(MainActivity.kt:9)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5279)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5187)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:57)
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:5238)
at android.app.ActivityThread.access$3400(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2026)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
However, I'm using appcompat theme on the application and the overlay.但是,我在应用程序和叠加层上使用了 appcompat 主题。
Any ideas how can I make it work?任何想法我怎样才能让它发挥作用?
NOTE: I tried to run it on Android 9 and Android 10注意:我尝试在 Android 9 和 Android 10 上运行它
Update 1: when i extend from Activity instead of AppCompatActivity it's working fine without any problems更新 1:当我从 Activity 而不是 AppCompatActivity 扩展时,它工作正常,没有任何问题
You can use my library in there I tried to do that in a very reusable way so that you can do an overlay with less code.你可以在那里使用我的库,我试图以一种非常可重用的方式做到这一点,这样你就可以用更少的代码进行覆盖。 Here is documentation on how to use it in Java and Kotlin .
这是有关如何在Java和Kotlin中使用它的文档。 First, initialize it from an activity from where you want to show the watermark-
首先,从要显示水印的活动中对其进行初始化-
AppWaterMarkBuilder.doConfigure()
.setAppCompatActivity(MainActivity.this)
.setWatermarkProperty(R.layout.layout_water_mark)
.showWatermarkAfterConfig();
Then you can hide and show it from anywhere in your app -然后,您可以在应用程序的任何位置隐藏和显示它 -
/* For hiding the watermark*/
AppWaterMarkBuilder.hideWatermark()
/* For showing the watermark*/
AppWaterMarkBuilder.showWatermark()
The gif -动图——
In your RRO module you should remove the dependency on AppCompat and the one one MaterialComponents.在您的 RRO 模块中,您应该删除对 AppCompat 和一个 MaterialComponents 的依赖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.