0

私は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 でエミュレーターを使用しています。

4

1 に答える 1

5

問題は、ウィンドウと contentView の間の混乱にあると思います。

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

一般に、これらの属性は、おそらく RelativeLayout や、コンテンツ ビューに配置するものによって尊重されません。

ウィンドウ属性は、ウィンドウのプロパティです。テーマを使用して、コードまたはアクティビティのセットアップで Window を変更できます。

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

次に、いくつかのresファイルで:

プロジェクト/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>
....

設定方法と同様に、アクティビティのウィンドウでこれらのプロパティを直接getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);設定して、ウィンドウを透明な背景に設定することもできます。

于 2012-09-17T00:59:03.373 に答える