【问题标题】:Runtime overlays android with appcompat运行时用 appcompat 覆盖 android
【发布时间】:2020-07-20 09:35:33
【问题描述】:

我想为使用 appcompat 的应用程序制作运行时覆盖,但我无法让它工作。

我关注了这个Example

首先: 我创建了我想要覆盖的 android 应用程序。我正在尝试将文本视图“Hello World”中的字符串覆盖为“Hello World overlay!”

应用代码快照

主要活动:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

主要活动布局:

<?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>

清单:

<?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>

应用主题:


    <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 name="hello_world">Hello World!</string>

到目前为止,代码运行良好:)

然后我创建包含覆盖资源的覆盖项目。

Overlay 项目代码快照:

清单:

<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>

我想在运行时覆盖它们的字符串资源

    <string name="hello_world">Hello World Overlay!</string>

我构建并安装了 apk。当我使用 adb shell cmd overlay list 时,我可以找到它作为叠加层

一旦我使用 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) 

但是,我在应用程序和叠加层上使用 appcompat 主题。

有什么想法可以让它发挥作用吗?

注意:我尝试在 Android 9 和 Android 10 上运行它

更新 1: 当我从 Activity 而不是 AppCompatActivity 扩展时,它工作正常,没有任何问题

【问题讨论】:

  • 我也面临这个问题。您找到任何可行的解决方案了吗?

标签: android overlay oms rro


【解决方案1】:

您可以在其中使用我的library,我尝试以一种非常可重用的方式做到这一点,这样您就可以用更少的代码进行覆盖。 这是有关如何在 JavaKotlin 中使用它的文档。首先,从要显示水印的活动中对其进行初始化-

AppWaterMarkBuilder.doConfigure()
                .setAppCompatActivity(MainActivity.this)
                .setWatermarkProperty(R.layout.layout_water_mark)
                .showWatermarkAfterConfig();

然后您可以在应用中的任何位置隐藏和显示它 -

  /* For hiding the watermark*/
  AppWaterMarkBuilder.hideWatermark() 

  /* For showing the watermark*/
  AppWaterMarkBuilder.showWatermark() 

gif -

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多