public class SlidingMenu extends HorizontalScrollView {
private LinearLayout mWapper;
private ViewGroup mMenu;
private ViewGroup mContent;
private int mScollWith;
// dp
private int mMenuRighPadding = 50;
private Boolean once;
private int mMenuWidth;
public SlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub未使用自定义属时 滴调用 AttributeSet 两个参数
// onMeasuer 决定内部View(子view)的宽和高 ,以及自己的宽和高
// onLayout 决定子控件的位置
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
mScollWith = outMetrics.widthPixels;
// 五十dp转化为 sp
mMenuRighPadding = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources()
.getDisplayMetrics());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
if (!once) {
mWapper = (LinearLayout) getChildAt(0);
mMenu = (ViewGroup) mWapper.getChildAt(0);
mContent = (ViewGroup) mWapper.getChildAt(1);
mMenuWidth = mMenu.getLayoutParams().width = mScollWith
- mMenuRighPadding;
mContent.getLayoutParams().width = mScollWith;
once = true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
相关分类