Beispiel #1
1
  /**
   * 为 DrawerLayout 布局设置状态栏透明
   *
   * @param activity 需要设置的activity
   * @param drawerLayout DrawerLayout
   */
  public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
      return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
    } else {
      activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

    ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
    // 内容布局不是 LinearLayout 时,设置padding top
    if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
      contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
    }

    // 设置属性
    ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
    drawerLayout.setFitsSystemWindows(false);
    contentLayout.setFitsSystemWindows(false);
    contentLayout.setClipToPadding(true);
    drawer.setFitsSystemWindows(false);
  }
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    Boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(Gravity.RIGHT);
    Boolean isMenuListFocused = false;
    if (mList != null) {
      isMenuListFocused = mList.hasFocus();
    }
    View view = this.getCurrentFocus();

    if (isDrawerOpen) {
      if (!view.getClass()
          .getName()
          .equalsIgnoreCase("com.android.internal.view.menu.ActionMenuItemView")) {
        mDrawerLayout.closeDrawers();
        mDrawerLayout.getChildAt(0).requestFocus();
      }
    } else {
      if (KeyEvent.KEYCODE_DPAD_UP == keyCode) {
        // action bar
        mDrawerLayout.getChildAt(1).requestFocus();
      } else if (KeyEvent.KEYCODE_DPAD_DOWN == keyCode) {
        // grid view
        mDrawerLayout.getChildAt(0).requestFocus();
      } else if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode) {
      } else if (KeyEvent.KEYCODE_MENU == keyCode) {
        if (!mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
          mDrawerLayout.openDrawer(Gravity.RIGHT);
        } else {
          mDrawerLayout.closeDrawer(Gravity.RIGHT);
        }
      }
    }

    return super.onKeyDown(keyCode, event);
  }
Beispiel #3
0
 /**
  * 为 DrawerLayout 布局设置状态栏透明(5.0以上半透明效果,不建议使用)
  *
  * @param activity 需要设置的activity
  * @param drawerLayout DrawerLayout
  */
 public static void setTranslucentForDrawerLayoutDiff(
     Activity activity, DrawerLayout drawerLayout) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
     // 设置状态栏透明
     activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
     // 设置内容布局属性
     ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
     contentLayout.setFitsSystemWindows(true);
     contentLayout.setClipToPadding(true);
     // 设置抽屉布局属性
     ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);
     vg.setFitsSystemWindows(false);
     // 设置 DrawerLayout 属性
     drawerLayout.setFitsSystemWindows(false);
   }
 }
Beispiel #4
0
 /**
  * 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用)
  *
  * @param activity 需要设置的activity
  * @param drawerLayout DrawerLayout
  * @param color 状态栏颜色值
  */
 public static void setColorForDrawerLayoutDiff(
     Activity activity, DrawerLayout drawerLayout, int color) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
     activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
     // 生成一个状态栏大小的矩形
     View statusBarView = createStatusBarView(activity, color);
     // 添加 statusBarView 到布局中
     ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
     contentLayout.addView(statusBarView, 0);
     // 内容布局不是 LinearLayout 时,设置padding top
     if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
       contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
     }
     // 设置属性
     ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
     drawerLayout.setFitsSystemWindows(false);
     contentLayout.setFitsSystemWindows(false);
     contentLayout.setClipToPadding(true);
     drawer.setFitsSystemWindows(false);
   }
 }
  private void init() {
    mMyDrawer = new MyDrawer(this);
    mDrawerLayout = mMyDrawer.getDrawerLayout();

    // getActionBar().setDisplayHomeAsUpEnabled(false);
    LayoutInflater inflater = getLayoutInflater();
    RelativeLayout container = (RelativeLayout) findViewById(R.id.frame_container);
    inflater.inflate(R.layout.activity_main_two_way_grid, container);

    horzGridView = (TwoWayGridView) findViewById(R.id.horz_gridview);
    horzGridView.setPadding(0, 0, 0, 0);
    List<DataObject> horzData = generateGridViewObjects();

    horzGridViewAdapter = new TelepadGridViewAdapter(mContext, horzData);
    mList = (ListView) findViewById(R.id.list_slidermenu);

    horzGridView.setAdapter(horzGridViewAdapter);
    horzGridView.setOnItemClickListener(this);
    // horzGridView.requestFocus();
    mDrawerLayout.getChildAt(0).requestFocus();
  }