2016-04-01 16 views
0

私の考えは、通常の通知のための単純なレイアウトと、アニメーション付きのポップアップのダイアログコントロールを持つことでした。ポップアップはユーザがクリックする位置にある必要があるため、Dialogueクラスを拡張したカスタムのDialogueViewを作成して、アンカーポイントを決定したいと考えました。レイアウトのAndroidにオーバーレイを表示する

デザインをポップアップせず:

pre_session_view_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:verizon="http://schemas.android.com/apk/res-auto" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:id="@+id/preSessionLayout"> 
<TextView 
    android:id="@id/verizon_sd_caption" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_toLeftOf="@id/verizon_sd_empty_view" 
    android:layout_toRightOf="@id/verizon_sd_icon" 
    android:gravity="center|center_horizontal" 
    android:paddingLeft="5dp" 
    android:paddingRight="10dp" 
    android:textColor="@android:color/white" 
    android:textSize="18sp"></TextView> 

<demo.notification.verizon.com.notificationdemo.ResizableImageView 
    android:id="@id/verizon_sd_icon" 
    android:layout_width="36dp" 
    android:layout_height="34dp" 
    android:paddingLeft="6dp" 
    android:paddingRight="6dp" 
    android:paddingTop="6dp" 
    android:paddingBottom="6dp" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:scaleType="centerInside" /> 
<!--android:src="@drawable/_fw_prenotify" --> 
<ImageView 
    android:id="@id/verizon_sd_empty_view" 
    android:layout_width="1dp" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:visibility="invisible" 
    /> 

</RelativeLayout> 

オーバーレイの設計:

これは私がこれまでにやっていることですPressionView.java:(通常とポップアップ通知を表示するためのJavaファイル)

私はオーバーレイとしてポップアップを表示するためのコードスニペットを与えています。この関数は、ユーザーがハチアイコンをクリックすると呼び出されます。次のように

private void showOverLay(){ 

    final ConfirmBox dialog = new ConfirmBox(this.bannerThumb); 
    LayoutInflater inflater = LayoutInflater.from(ctx); 
    dialog.onCreateView(inflater,this.relLayout,null); 

} 

最後にカスタム対話は次のとおりです。

public class ConfirmBox extends DialogFragment { 
private View source; 

public ConfirmBox() { 
} 

public ConfirmBox(View source) { 
    this.source = source; 
} 

public static ConfirmBox newInstance(View source) { 
    return new ConfirmBox(source); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setStyle(STYLE_NO_FRAME, R.style.AppTheme); 
} 


@Override 
public void onStart() { 
    super.onStart(); 

    // Less dimmed background; see http://stackoverflow.com/q/13822842/56285 
    Window window = getDialog().getWindow(); 
    WindowManager.LayoutParams params = window.getAttributes(); 
    params.dimAmount = 0.2f; // dim only a little bit 
    window.setAttributes(params); 

    // Transparent background; see http://stackoverflow.com/q/15007272/56285 
    // (Needed to make dialog's alpha shadow look good) 
    window.setBackgroundDrawableResource(android.R.color.transparent); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // Put your dialog layout in R.layout.view_confirm_box 
    View view = inflater.inflate(R.layout.overlay_view, container, false); 

    // Initialise what you need; set e.g. button texts and listeners, etc. 

    // ... 

    setDialogPosition(); 

    return view; 
} 

/** 
* Try to position this dialog next to "source" view 
*/ 
private void setDialogPosition() { 
    if (source == null) { 
     return; // Leave the dialog in default position 
    } 

    // Find out location of source component on screen 
    // see http://stackoverflow.com/a/6798093/56285 
    int[] location = new int[2]; 
    source.getLocationOnScreen(location); 
    int sourceX = location[0]; 
    int sourceY = location[1]; 

    Window window = getDialog().getWindow(); 

    // set "origin" to top left corner 
    window.setGravity(Gravity.TOP| Gravity.LEFT); 

    WindowManager.LayoutParams params = window.getAttributes(); 

    // Just an example; edit to suit your needs. 
    params.x = sourceX - dpToPx(110); // about half of confirm button size left of source view 
    params.y = sourceY - dpToPx(80); // above source view 

    window.setAttributes(params); 
} 

public int dpToPx(float valueInDp) { 
    DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics(); 
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics); 
} 
} 

私は通常の通知を表示することができます。しかし、私が取ったポップアップのアプローチでは、showOverLay()メソッドでnullポインタ例外が発生します。

具体線上:

ウィンドウウィンドウ= getDialog()は、GetWindow()。

内のカスタムDialogueclassのgetDialoguePosition()内にあります。

誰かが親切に私を助けることができますか? reqdの場合は、PresessionView.javaファイルの完全なコードも共有できます。

カスタムDialogueクラスを呼び出す際に間違いがあります。

ありがとうございました。

答えて

0

ダイアログのフラグメントが正しく表示されません。 dialog.show(...)をご利用ください。また、bannerThumb、ctx、およびrelLayoutがnullでないことを確認してください。

private void showOverLay(){ 
    final ConfirmBox dialog = new ConfirmBox(this.bannerThumb); 
    LayoutInflater inflater = LayoutInflater.from(ctx); //Ensure ctx is not NULL 
    dialog.show(getSupportFragmentManager(), "yourtitle"); 
} 
+0

ありがとうございました。 showOverlay()メソッドからカスタムのDialogueクラスを呼び出す方法の例を親切に教えてください。 – user2284140

+0

あなたの参照のために上記のコメントが更新されました。 DialogFragmentのサンプルコードは、ネット全体で見つけることもできます。 – ekouChiq

関連する問題