Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

android - Activity should be transparent, but has black background

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>
  </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')

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...