您的位置:首页 > 运维架构

华为手机使umeng分享popwindow被底部导航栏挡住问题

2015-12-20 15:57 405 查看
本人也是刚学Android不久,刚刚实习在工作中遇到umeng分享被底部导航栏挡住的问题,不知道怎么解决,后来经过多番纠结在巧合的情况下发下来了这个方法,验证了一些手机发现可以,所以在此和大家分享一下。

其实这个问题也很简单就是在被挡住的手机上面做一个popwindow的偏移,但问题是如何判断在那些手机上需要偏移,总不能在所有手机上都偏移吧

解决方法:

比较decorview与screenview的高度 如果decorview大于screenview

则 popupWindow.showAtLocation(linearLayout, Gravity.BOTTOM, 0, -getNavigationBarHeight());

否则popupWindow.showAtLocation(linearLayout, Gravity.BOTTOM, 0, 0);

这样就解决了,哈哈哈

然而你真的以为这样就行了吗?并没有 过几天测试妹子就来找我了 说我程序有bug popwindow还是会被底部导航栏挡住,我就说不可能啊,我明明就测试过了,而且显示正常。所以我就去看看她到底是怎么测试的,结果发现,你妹 华为手机的底部导航栏是可以设置隐藏的。所以我写的程序果然有问题。如果隐藏导航栏,在弹出popwindow时再将导航栏显示出来会挡住popwindow,而如果一开始将导航栏显示,弹出popwindow后再将导航栏隐藏,底部就会空出一部分,非常难看。所以还是要接着改。

解决方法:

每次导航栏消失或者显示都会使view重新绘制 所以我在popwindow的contentview(我的是linearlayout)上做了些改动,在这个contentview的Onlayout方法里面判断是否需要偏移,如果偏移就设置这个contentview.setPadding(0,0,0,getNavigationBarHeight());不偏移就正常显示contentview.setPadding(0,0,0,0);

代码:

/**
* Created by wyq on 2015/12/16.
*/
public class MyLinearLayout extends LinearLayout {
private static final String TAG ="CustomShareBoard";
private Context context;
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context= context;

}

public MyLinearLayout(Context context) {
super(context);
this.context= context;
}

public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context= context;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(!isOffset()) {
L.i(TAG,"不需要偏移");
if (this.getPaddingBottom()!=0) {
this.setPadding(0, 0, 0, 0);
}
} else{
L.i(TAG, "需要偏移");
this.setPadding(0, 0, 0, getNavigationBarHeight());
invalidate();//这个一定要加上 不然会出错 我也不知道为什么如果有人找到原因请回复我 在此先谢过了
}
super.onLayout(true, l, t, r, b);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}

private int getNavigationBarHeight() {
Resources resources = this.context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}
public boolean isOffset() {
return getDecorViewHeight() > getScreenHeight();
}

public int getDecorViewHeight(){
return ((Activity)this.context).getWindow().getDecorView().getHeight();
}

public int getScreenHeight(){
DisplayMetrics dm = new DisplayMetrics();
((Activity)this.context).getWindowManager().getDefaultDisplay().getMetrics(dm);//当前activity
return dm.heightPixels;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: