开发者

Activity should be transparent, but has black background

开发者 https://www.devze.com 2022-12-27 22:48 出处:网络
My use case is writing an overlay controller activity for a landscape camera preview. I followed the instructions from a couple of tutorials for writing a transparent theme.

My use case is writing an overlay controller activity for a landscape camera preview. I followed the instructions from a couple of tutorials for writing a transparent theme.

So my res/values/style.xml looks like this:

<resources>

  <style name="Theme" parent="android:Theme" />

  <style name="Theme.Transparent">
      <item name="android:windowBackground">@drawable/transparent_background</item>
开发者_如何学Python  </style>

  <drawable name="transparent_background">#00000000</drawable>

</resources>

The activity snippet:

    <activity android:name=".CameraPreview"
       android:label="Camera"
       android:screenOrientation="landscape"
       android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Controlls"
              android:label="Controlls"
              android:screenOrientation="portrait"
              android:theme="@android:style/Theme.Translucent">
    </activity>

When I start this activity from my root activity, the layout gets drawn correctly, but the background stays black. I tried to use @android:style/Theme.Translucent instead, but this Theme inherits the orientation from the calling activity (landscape) and thats not what I want.

Edit:

The application holding the camera preview is set to landscape view as it does not display the preview correctly in portrait orientation. (see old google bug report)

What I wanted to do was to put an independent activity for user interaction interface in front of the camera surface holder (this activity should be set to 'portrait', or even better to 'sensor')


Here's a somewhat related answer for anyone else that has a similar problem.

tl;dr
Adding a new style where the name is a suffix of an existing style can cause problems (like making transparent activities have a black screen).


Problem: Our transparent activity background was black after doing a large refactor. (The activity was already using a transparent theme.)

After several hours of going through commits I found what seemed to be the cause of the problem. In our app we use styles like CSS. There was an existing style like this that we applied to a TextView.

<style name="HeadLine.SM.White.MyFontBold">
    <item name="android:fontFamily">@font/some_bold_font</item>
</style>

The non-bold variant of the style was added as a style before the bold variant as below.

//This style was added
<style name="HeadLine.SM.White.MyFont">
    <item name="android:fontFamily">@font/some_font</item>
</style>

<style name="HeadLine.SM.White.MyFontBold">
    <item name="android:fontFamily">@font/some_bold_font</item>
</style>

For some reason, after the non-bold style variant was added all transparent activities had a black screen. When we changed the non-bold variant style name to something else it fixed the problem for us. Now our styles look like this (I know there are better ways to handle font styles - these styles are a few years old).

<style name="HeadLine.SM.White.MyFontRegular">
    <item name="android:fontFamily">@font/some_font</item>
</style>

<style name="HeadLine.SM.White.MyFontBold">
    <item name="android:fontFamily">@font/some_bold_font</item>
</style>

Conclusion

It seemed that adding a new style where the name is a suffix of an existing style caused problems. If you're adding a new style make sure the name is not a suffix of an existing style.

We did try cleaning the build, rebuilding, and invalidating Android Studio caches. None of these things solved our problem.


Try the built-in @android:style/Theme.Translucent instead of your custom one, according to this blog post. I haven't tried to do this myself, so I don't know if the technique written about there works or not.


I found out, that another important child element for the style description above is <item name="android:windowIsTranslucent">true</item> was lacking.

Problem:

That child element also causes synchronizing the activity's orientation with the calling one. (same effect as @android:style/Theme.Translucent)


I bumped into the same problem as you describe (regarding translucent background and screen orientation), but in the end I reconciled with the fact that this is just how it works. In fact it actually makes sense that it works this way. I can't think of any screen based system that supports mixing portrait and landscape views, so why should Android?

I guess the general rule is that all visible activities must have the same orientation, regardless of the attributes in the manifest-file. If all are set to "sensor" then they will all change, if one is fixed to portrait or landscape, then the others must follow (and whoever sets it last "wins").

I guess this was just so obvious to the developers that it didn't occur to them to document it :)


Remove and all its done

@Override
public void onAttachedToWindow()  {}


Style name is having some effect on the transparent background to be black. I used below one and works fine.

<style name="Theme.AppCompat.Translucent">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

<!-- AndroidManifest.xml -->
<activity android:name=".TranslucentThemeActivity"
    android:theme="@style/Theme.AppCompat.Translucent"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号