猿问

仿QQ5.0的自定菜单,在子控件报空指针异常,子类控件不能获取,mWapper = (LinearLayout) getChildAt(0)

 

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);
 }

寒虞羲墨逸
浏览 1077回答 0
0回答
随时随地看视频慕课网APP

相关分类

Android
我要回答