我的想法是为普通通知设置一个简单的布局,并为弹出动画提供一个对话框控件。由于弹出窗口需要在用户点击的位置,我想编写一个扩展Dialog类的自定义DialogueView,我在其中确定锚点。 这是我到目前为止所做的:
没有弹出窗口的布局设计:
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>
叠加层的设计:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="240dp"
android:layout_height="110dp"
android:orientation="vertical"
android:id="@+id/overlayLayout">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/notification_bg_green"/>
</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()方法中得到一个空指针异常。
更具体地说:
Window window = getDialog()。getWindow();
在自定义Dialogueclass的getDialoguePosition()中。
有人可以帮助我吗?如果需要,我也可以共享PresessionView.java文件的完整代码。
我认为我在调用自定义Dialogue类时犯了一些错误。
感谢。
答案 0 :(得分:0)
您显示的对话框片段不正确。
请使用dialog.show(...)
。另外请确保您的bannerThumb,ctx和relLayout不为空。
private void showOverLay(){
final ConfirmBox dialog = new ConfirmBox(this.bannerThumb);
LayoutInflater inflater = LayoutInflater.from(ctx); //Ensure ctx is not NULL
dialog.show(getSupportFragmentManager(), "yourtitle");
}