【问题标题】:Android transparent layout安卓透明布局
【发布时间】:2012-09-18 01:35:10
【问题描述】:

我对 Android 很陌生。我想在用户接到电话时弹出一个透明屏幕。我有这段代码可以打开 MyActivity 屏幕,但它是白色的而不是透明的。

public class CallReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
      SystemClock.sleep(1);
      Intent intent = new Intent(context, MyActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
      context.startActivity(intent);
    }

  }

}

这是 MyActivity 的代码:

public class MyActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_my);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
  }
}

这是布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:windowBackground="@android:color/transparent" 
    android:windowIsTranslucent="true" 
    android:windowAnimationStyle="@android:style/Animation.Translucent" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/incoming_call"
        tools:context=".MyActivity" />
</RelativeLayout>

这成功地弹出了一个带有我的消息的屏幕,但它使用白色背景而不是透明背景。知道我可能做错了什么吗?我正在使用带有 Android 2.2 SDK 的模拟器。

【问题讨论】:

    标签: android android-layout transparency


    【解决方案1】:

    我认为问题可能在于 window 和 contentView 之间的混淆。

    android:windowBackground="@android:color/transparent" 
    android:windowIsTranslucent="true" 
    android:windowAnimationStyle="@android:style/Animation.Translucent"
    android:windowNoTitle="true"
    android:windowFrame="@null"
    

    一般来说,RelativeLayout 和您在内容视图中放置的任何内容都可能不会尊重这些属性。

    窗口属性是窗口的一个属性。您可以在代码中或使用主题在活动设置中更改窗口。

    <activity android:name="....YourActivity" android:theme="@style/MyTransparentTheme"/>
    

    然后在一些 res 文件中:

    Project/res/values/themes.xml

    <resources ....>
     ....
    <style android:name="MyTransparentTheme" parent="@android:style/Theme">
      <item name="android:windowBackground">@android:color/transparent</item>
      <item name="android:windowIsTranslucent">true</item>
      <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
      <item name="android:windowNoTitle">true</item>
      <item name="android:windowFrame">@null</item>
    </style>
    ....
    

    你也可以直接在activity的Window上设置这些属性来设置,类似于你设置getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);你也可以只设置window为透明背景。

    【讨论】:

    • 谢谢。恐怕我现在只有黑色背景。我真的很想在来电屏幕上方显示一个透明背景,以便您仍然可以看到接听和挂断控制。如果我用 android:theme="@android:style/Theme.Dialog" 替换我的主题或者我设置 parent="@android:style/Theme.Dialog" 那么我可以在我的消息下方看到来电屏幕,但它也添加其他我不想要的样式,例如边框、灰色背景和下方屏幕的模糊。我可以从对话框主题中获取一些可以添加透明背景的样式吗?
    • 更新了代码以具有正确的 xml 格式(哎呀,太晚了),还添加了一些其他参数来关闭窗口。这肯定会奏效,或者至少可以让您找到所需的正确方向。
    • 非常感谢您的帮助!我不得不用 name="MyTransparentTheme" 替换 android:name="MyTransparentTheme" (也许这是因为我把它放在 styles.xml 而不是 theme.xml 中?)
    • 我认为资源文件不使用命名空间,或者只是假设根命名空间是 android。我猜这取决于您如何设置文件。
    猜你喜欢
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多