Android疑难杂症-7.0系统PopupWindow#showAsDropDown()显示异常

问题描述

1
2
3
final PopupWindow popupWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//... ...
popupWindow.showAsDropDown(anchor);

上述代码在7.0手机上不会在anchor下方弹出,而是在屏幕最上方弹出,7.0以下手机则没问题;
高度MATCH_PARENT时才会这样,WRAP_CONTENT时不会。

原因

  • 7.0系统的一个bug;

Android常用代码-EditText各种样式背景

底部变色直线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="1px" android:state_focused="true">
<layer-list>
<item>
<shape>
<solid android:color="@color/colorPrimary" />
</shape>
</item>
<item android:bottom="1px">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
</item>
<item android:bottom="1px" android:state_pressed="false">
<layer-list>
<item>
<shape>
<solid android:color="#ddd" />
</shape>
</item>
<item android:bottom="1px">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
</item>
</selector>

Android常用代码-发送验证码倒计时

使用RxJava优雅的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
btnVerifyCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnVerifyCode.setEnabled(false);
Observable.interval(0, 1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.limit(31)
.map(new Func1<Long, Long>() {
@Override
public Long call(Long aLong) {
return 30 - aLong;
}
})
.doOnSubscribe(new Action0() {
@Override
public void call() {
btnVerifyCode.setEnabled(false);
}
})
.doOnCompleted(new Action0() {
@Override
public void call() {
btnVerifyCode.setEnabled(true);
btnVerifyCode.setText("重新发送");
}
})
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
btnVerifyCode.setText("重新发送("+aLong + " s )");
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
ToastUtils.showCustomBgToast("获取失败,错误信息:" + throwable.getMessage() + "");
}
});
}
});

Android常用代码-底部弹出PopupWindow

关键代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 显示自定义底部弹出PopupWindow
*
* @param parent 传根布局就好了
* @param contentView PopupWindow内容
* @param bgColor 背景颜色(窗体变暗效果)
* @param focusable 是否点击空白区域取消(非全屏时可以)和返回键关闭
* @param fullScreen 是否全屏
* @param onDismissListener 取消监听
*/
public void showBottomPopupWindow(View parent, View contentView, int bgColor, boolean focusable, boolean fullScreen, PopupWindow.OnDismissListener onDismissListener) {
hideBottomDialog();
AutoUtils.auto(contentView);
popupWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, fullScreen ? ViewGroup.LayoutParams.MATCH_PARENT : ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(contentView);
// 设置弹出窗体可点击
popupWindow.setFocusable(focusable);
// 设置弹出窗体的背景
popupWindow.setBackgroundDrawable(new ColorDrawable(bgColor));
if (onDismissListener != null) {
popupWindow.setOnDismissListener(onDismissListener);
}
//设置弹出窗体显示时的动画,从底部向上弹出
popupWindow.setAnimationStyle(R.style.ZzAnimationDialog);
popupWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
/**
* 隐藏自定义底部弹出PopupWindow
*/
public void hideBottomDialog() {
if (popupWindow != null) {
popupWindow.dismiss();
popupWindow = null;
}
}

Android自定义控件-SlidingValidationView

效果图

图2

代码

属性

1
2
3
4
5
6
7
8
9
<declare-styleable name="SlidingValidationView">
<attr name="sv_drag_view_color" format="color|reference" />
<attr name="sv_line_color" format="color|reference" />
<attr name="sv_final_point_color" format="color|reference" />
<attr name="sv_sliding_progress" format="integer" />
<attr name="sv_draw_radius" format="dimension|reference" />
<attr name="sv_final_point_radius" format="dimension|reference" />
<attr name="sv_line_point_radius" format="dimension|reference" />
</declare-styleable>

Android自定义控件-RatioColorBar

效果图

图2

代码

属性

1
2
3
4
5
6
7
<declare-styleable name="RatioColorBar">
<attr name="rcb_show_border" format="boolean" />
<attr name="rcb_show_padding" format="boolean" />
<attr name="rcb_border_width" format="dimension|reference" />
<attr name="rcb_border_padding" format="dimension|reference" />
<attr name="rcb_border_color" format="color|reference" />
</declare-styleable>