1

だから私はを拡張するこのクラスを持っていますPopupWindow。クラスの背景をに設定し、その@android:color/transparent内容に次のレイアウトを使用します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tips_root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dip"
    android:orientation="horizontal"
    style="?Tips_Background" >

    <TextView
        android:id="@+id/textInfo"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/register_termsandconditions" 
        style="?TextAppearance_Footer" />

    <ImageButton
        android:id="@+id/btnClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="0dip"
        android:layout_gravity="top|right"
        android:src="@drawable/btn_dialog_normal" 
        style="@style/App_ImageButtonStyle" />

</LinearLayout>

アイデアは次のようなポップアップを取得することです:

私のカスタムヒントウィンドウ

この効果を実現するために使用するスタイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:visible="false">

    <gradient 
        android:startColor="@color/trademark_dark"
        android:endColor="@color/trademark_darkest"
        android:angle="90" />

    <stroke android:width="2dp" android:color="#AAAAAA" />
    <corners android:radius="10dp" />

    <padding 
        android:left="8dp" 
        android:top="8dp"
        android:right="8dp" 
        android:bottom="8dp" />

</shape>

背景がまったく効果がないかのようにウィンドウが完全に透明に見える私のスタートアップアクティビティを除いて、物事は期待どおりに機能しています。

私の透明なヒントウィンドウ

このスタイルを含むテーマを、以下を使用してアプリケーション全体に適用しています。

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:theme="@style/NI_AppTheme.Trademark"
    android:name="NApplication">
    <activity android:name="LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  ....

LoginActivity私がこの問題に直面している唯一の活動です。何が欠けている可能性がありますか?

アップデート:

@imrankhanによって概説された解決策は機能します。ただし、背景はテーマによって異なるため、直接ハードコードされた参照ではなく、テーマ属性を介してこの値を参照する必要があります。私は試した:

<style name="NI_AppTheme.Trademark">
    <item name="Tips_Background">@drawable/application_tips_trademark</item>
</style>

次に、レイアウトを設定しますandroid:background="?Tips_Background"が、次のスタックトレースでアプリケーションがクラッシュします。

 06-25 21:09:36.518: E/AndroidRuntime(874): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f01002e a=-1}
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.content.res.Resources.loadDrawable(Resources.java:1897)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.view.View.<init>(View.java:2785)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.view.ViewGroup.<init>(ViewGroup.java:385)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.widget.LinearLayout.<init>(LinearLayout.java:174)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.widget.LinearLayout.<init>(LinearLayout.java:170)
4

3 に答える 3

1

アクティビティは、マニフェストで定義されたテーマが実際にアタッチされる唯一のコンテキストです。他のインスタンスは、システムのデフォルトテーマを使用してビューを膨らませ、おそらく予期しない表示出力をもたらします。

コンテキスト、どのようなコンテキスト?

getApplicationContext()これが、テーマを適切に設定しない理由です。

于 2013-06-22T02:13:43.760 に答える
0

Tips_root backgroundshapxmlをres/ drawableに配置します。android:background次のように代わりに使用style="?Tips_Background"します。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tips_root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dip"
    android:orientation="horizontal"
    android:background="@drawable/Tips_Background" >
于 2012-06-25T12:38:57.977 に答える
0

getApplicationContext()最終的には、PopupWindowコンストラクターに返されたコンテキストを渡すことに問題を絞り込みました。コンテキストとして渡すLoginActivity.thisと、期待どおりに機能し、親アクティビティで選択したテーマに応じて背景を変更できます。

それでも、なぜgetApplicationContext()私が設定したテーマがないのか、私を打ち負かします。思考の糧。

于 2012-06-30T12:59:27.183 に答える