【问题标题】:Android statusbar color in Embarcadero RAD Studio, C++Embarcadero RAD Studio,C ++中的Android状态栏颜色
【发布时间】:2016-12-25 06:01:01
【问题描述】:

编辑:

我设法使用此代码删除了状态栏的当前背景(以及导航栏的背景)

ANativeActivity_setWindowFlags(PANativeActivity(System::DelphiActivity),
TJWindowManager_LayoutParams::JavaClass->FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
TJWindowManager_LayoutParams::JavaClass->FLAG_TRANSLUCENT_STATUS);

仍然不知道如何添加新的背景颜色

这不起作用:

_di_JWindow androidWindow =  SharedActivity()->getWindow();
androidWindow->setStatusBarColor(17170432);

================================================ =======================

原帖:

我正在尝试为使用 FireMonkey/RAD Studio 制作的 Android 应用的状态栏着色。

为了在 Java 中实现它,我通常使用以下代码:

Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(R.color.my_statusbar_color));

在 RAD Studio 中,我正在尝试这样做

首先我在标头中包含 Android 库:

#ifdef __ANDROID__
#include <FMX.Helpers.Android.hpp>
#include <Androidapi.JNIBridge.hpp>
#include <Androidapi.JNI.Os.hpp>
#include <Androidapi.JNI.JavaTypes.hpp>
#include <Androidapi.JNI.GraphicsContentViewText.hpp>
#include <Androidapi.JNI.Util.hpp>
#include <Androidapi.JNI.App.hpp>
#include <Androidapi.JNI.Embarcadero.hpp>
#include <Androidapi.Jni.hpp>
#endif

然后当我试图打电话时:

JWindow androidWindow =  JNativeActivity::getWindow();

我收到超过 100 个错误,如下所示:

[bccaarm Error] HeaderFooterTemplate.cpp(67): call to non-static member function without an object argument
[bccaarm Error] HeaderFooterTemplate.cpp(67): variable type 'Androidapi::Jni::Graphicscontentviewtext::JWindow' is an abstract class
  sysmac.h(327): unimplemented pure virtual method 'QueryInterface' in 'JWindow'
  sysmac.h(328): unimplemented pure virtual method 'AddRef' in 'JWindow'
  sysmac.h(329): unimplemented pure virtual method 'Release' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(763): unimplemented pure virtual method 'equals' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(764): unimplemented pure virtual method 'getClass' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(765): unimplemented pure virtual method 'hashCode' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(766): unimplemented pure virtual method 'notify' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(767): unimplemented pure virtual method 'notifyAll' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(768): unimplemented pure virtual method 'toString' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(769): unimplemented pure virtual method 'wait' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(770): unimplemented pure virtual method 'wait' in 'JWindow'
  Androidapi.JNI.JavaTypes.hpp(771): unimplemented pure virtual method 'wait' in 'JWindow'
  Androidapi.JNI.GraphicsContentViewText.hpp(18831): unimplemented pure virtual method 'addContentView' in 'JWindow'
  Androidapi.JNI.GraphicsContentViewText.hpp(18832): unimplemented pure virtual method 'addFlags' in 'JWindow'
  Androidapi.JNI.GraphicsContentViewText.hpp(18833): unimplemented pure virtual method 'clearFlags' in 'JWindow'
  Androidapi.JNI.GraphicsContentViewText.hpp(18834): unimplemented pure virtual method 'closeAllPanels' in 'JWindow'
  Androidapi.JNI.GraphicsContentViewText.hpp(18835): unimplemented pure virtual method 'closePanel' in 'JWindow'
  Androidapi.JNI.GraphicsContentViewText.hpp(18836): unimplemented pure virtual method 'findViewById' in 'JWindow'
  Androidapi.JNI.GraphicsContentViewText.hpp(18837): unimplemented pure virtual method 'getAllowEnterTransitionOverlap' in 'JWindow'

这是我使用 C++ 的第一步,所以我不确定我做得对。

【问题讨论】:

    标签: android c++ java-native-interface c++builder firemonkey


    【解决方案1】:

    您没有正确使用 Embarcadero 的 JNI 包装类。翻译看起来更像这样:

    _di_JActivity activity = SharedActivity();
    // or: _di_JActivity activity = TAndroidHelper::Activity;
    
    _di_JWindow window = activity->getWindow();
    window->clearFlags(TJWindowManager_LayoutParams::JavaClass->FLAG_TRANSLUCENT_STATUS);
    window->addFlags(TJWindowManager_LayoutParams::JavaClass->FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window->setStatusBarColor(activity->getResources()->getColor(...));
    // I don't know how to translate R.color.my_statusbar_color
    

    不过,话虽如此,Embarcadero 对 JWindow 接口的定义包括在 API 级别 21(Android 5 ,棒棒糖)。因此,除非您在自己的代码中为 Window 类手动定义自己的 JNI 接口,否则您将无法在 C++Builder 中调用 setStatusBarColor(),除非您在 Java 代码中进行调用是由您的 C++ 代码调用,或使用反射。

    1:除非它是在柏林 10.1 中添加的,我没有安装。它不存在于 10.0 西雅图。

    【讨论】:

    • setStatusBarColor() 现在在我使用的 10.1 中可用。以下代码不起作用window-&gt;clearFlags(TJWindowManager_LayoutParams::JavaClass-&gt;FLAG_TRANSLUCENT_STATUS); 它会导致异常Only the original thread that created a view hierarchy can touch its views. 相反我尝试过:ANativeActivity_setWindowFlags(PANativeActivity(System::DelphiActivity),TJWindowManager_LayoutParams::JavaClass-&gt;FLAG ... 在这一步我删除了状态栏的背景颜色 - 它是透明的,导航栏也是如此(不幸的是),并且可能会添加空白的 ActionBar。
    • “仅原始线程”错误是不言自明的。 Android UI 与创建它们的线程上下文相关联。您不能跨线程边界访问 UI 对象。如果您有一个工作线程试图操作由主线程创建的 UI,您必须委托主线程进行该操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2017-08-04
    • 2023-03-12
    • 1970-01-01
    • 2017-03-05
    • 2018-03-01
    相关资源
    最近更新 更多