/** @see android.view.ViewGroup.MarginLayoutParams#setMarginStart(int) */
 public static void setMarginStart(MarginLayoutParams layoutParams, int start) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
     layoutParams.setMarginStart(start);
   } else {
     layoutParams.leftMargin = start;
   }
 }
 public void restoreMarginLayoutParams(MarginLayoutParams params) {
   this.restoreLayoutParams(params);
   params.leftMargin = this.mPreservedParams.leftMargin;
   params.topMargin = this.mPreservedParams.topMargin;
   params.rightMargin = this.mPreservedParams.rightMargin;
   params.bottomMargin = this.mPreservedParams.bottomMargin;
   MarginLayoutParamsCompat.setMarginStart(
       params, MarginLayoutParamsCompat.getMarginStart(this.mPreservedParams));
   MarginLayoutParamsCompat.setMarginEnd(
       params, MarginLayoutParamsCompat.getMarginEnd(this.mPreservedParams));
 }
Exemple #3
0
 /**
  * 设置View的左侧外边距(像素值)
  *
  * @param view
  * @return
  */
 public static boolean setMarginLeft(View view, int marginLeft) {
   MarginLayoutParams marginParams = null;
   try {
     marginParams = (MarginLayoutParams) view.getLayoutParams();
   } catch (ClassCastException e) {
     return false;
   }
   if (marginParams == null) return false;
   marginParams.leftMargin = marginLeft;
   view.setLayoutParams(marginParams);
   return true;
 }
 public void show(int touchX, int touchY) {
   this.mL = touchX - this.mRegistrationX;
   this.mT = touchY - this.mRegistrationY;
   this.mR = (touchX - this.mRegistrationX) + this.mWidth;
   this.mB = (touchY - this.mRegistrationY) + this.mHeight;
   this.mLayout.addView(this);
   MarginLayoutParams mp = (MarginLayoutParams) getLayoutParams();
   mp.leftMargin = this.mL;
   mp.topMargin = this.mT;
   mp.rightMargin = this.mR;
   mp.bottomMargin = this.mB;
   setLayoutParams(mp);
 }
Exemple #5
0
 /**
  * 设置View的外边距(像素值)
  *
  * @param view
  * @param left
  * @param top
  * @param right
  * @param bottom
  * @return
  */
 public static boolean setMargin(View view, int left, int top, int right, int bottom) {
   MarginLayoutParams marginParams = null;
   try {
     marginParams = (MarginLayoutParams) view.getLayoutParams();
   } catch (ClassCastException e) {
     return false;
   }
   if (marginParams == null) return false;
   marginParams.leftMargin = left;
   marginParams.topMargin = top;
   marginParams.rightMargin = right;
   marginParams.bottomMargin = bottom;
   view.setLayoutParams(marginParams);
   return true;
 }
  private static void resetViewLayoutParams(View view, float scale) {

    if (view instanceof TextView) {
      resetTextSize((TextView) view, scale);
    }

    int pLeft = convertFloatToInt(view.getPaddingLeft() * scale);
    int pTop = convertFloatToInt(view.getPaddingTop() * scale);
    int pRight = convertFloatToInt(view.getPaddingRight() * scale);
    int pBottom = convertFloatToInt(view.getPaddingBottom() * scale);

    view.setPadding(pLeft, pTop, pRight, pBottom);

    LayoutParams params = view.getLayoutParams();
    if ("test_relayout".equals(view.getTag())) {
      Log.i("relayout", "test_relayout/" + params);
    }
    if (params == null) {
      return;
    }
    if (params.width > 0) {
      params.width = convertFloatToInt(params.width * scale);
    }
    if (params.height > 0) {
      params.height = convertFloatToInt(params.height * scale);
    }

    if (params instanceof MarginLayoutParams) {
      MarginLayoutParams mParams = (MarginLayoutParams) params;
      if (mParams.leftMargin > 0) {
        mParams.leftMargin = convertFloatToInt(mParams.leftMargin * scale);
      }
      if (mParams.rightMargin > 0) {
        mParams.rightMargin = convertFloatToInt(mParams.rightMargin * scale);
      }
      if (mParams.topMargin > 0) {
        mParams.topMargin = convertFloatToInt(mParams.topMargin * scale);
      }
      if (mParams.bottomMargin > 0) {
        mParams.bottomMargin = convertFloatToInt(mParams.bottomMargin * scale);
      }
    }
  }
    public void fillMarginLayoutParams(MarginLayoutParams params, int widthHint, int heightHint) {
      this.fillLayoutParams(params, widthHint, heightHint);
      this.mPreservedParams.leftMargin = params.leftMargin;
      this.mPreservedParams.topMargin = params.topMargin;
      this.mPreservedParams.rightMargin = params.rightMargin;
      this.mPreservedParams.bottomMargin = params.bottomMargin;
      MarginLayoutParamsCompat.setMarginStart(
          this.mPreservedParams, MarginLayoutParamsCompat.getMarginStart(params));
      MarginLayoutParamsCompat.setMarginEnd(
          this.mPreservedParams, MarginLayoutParamsCompat.getMarginEnd(params));
      if (this.leftMarginPercent >= 0.0F) {
        params.leftMargin = (int) ((float) widthHint * this.leftMarginPercent);
      }

      if (this.topMarginPercent >= 0.0F) {
        params.topMargin = (int) ((float) heightHint * this.topMarginPercent);
      }

      if (this.rightMarginPercent >= 0.0F) {
        params.rightMargin = (int) ((float) widthHint * this.rightMarginPercent);
      }

      if (this.bottomMarginPercent >= 0.0F) {
        params.bottomMargin = (int) ((float) heightHint * this.bottomMarginPercent);
      }

      if (this.startMarginPercent >= 0.0F) {
        MarginLayoutParamsCompat.setMarginStart(
            params, (int) ((float) widthHint * this.startMarginPercent));
      }

      if (this.endMarginPercent >= 0.0F) {
        MarginLayoutParamsCompat.setMarginEnd(
            params, (int) ((float) widthHint * this.endMarginPercent));
      }

      if (Log.isLoggable("PercentLayout", 3)) {
        Log.d(
            "PercentLayout",
            "after fillMarginLayoutParams: (" + params.width + ", " + params.height + ")");
      }
    }
 public void setContentX(final int contentX) {
   final MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams();
   layoutParams.leftMargin = contentX;
   setLayoutParams(layoutParams);
 }
  public void layout(Launcher launcher) {
    FrameLayout.LayoutParams lp;
    Resources res = launcher.getResources();
    boolean hasVerticalBarLayout = isVerticalBarLayout();

    // Layout the search bar space
    View searchBar = launcher.getSearchBar();
    lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
    if (hasVerticalBarLayout) {
      // Vertical search bar space
      lp.gravity = Gravity.TOP | Gravity.LEFT;
      lp.width = searchBarSpaceHeightPx;
      lp.height = LayoutParams.WRAP_CONTENT;

      LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
      targets.setOrientation(LinearLayout.VERTICAL);
    } else {
      // Horizontal search bar space
      lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
      lp.width = searchBarSpaceWidthPx;
      lp.height = searchBarSpaceHeightPx;
    }
    searchBar.setLayoutParams(lp);

    // Layout the workspace
    PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
    lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
    lp.gravity = Gravity.CENTER;
    int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
    Rect padding = getWorkspacePadding(orientation);
    workspace.setLayoutParams(lp);
    workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
    workspace.setPageSpacing(getWorkspacePageSpacing(orientation));

    // Layout the hotseat
    View hotseat = launcher.findViewById(R.id.hotseat);
    lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
    if (hasVerticalBarLayout) {
      // Vertical hotseat
      lp.gravity = Gravity.END;
      lp.width = hotseatBarHeightPx;
      lp.height = LayoutParams.MATCH_PARENT;
      hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
    } else if (isTablet()) {
      // Pad the hotseat with the workspace padding calculated above
      lp.gravity = Gravity.BOTTOM;
      lp.width = LayoutParams.MATCH_PARENT;
      lp.height = hotseatBarHeightPx;
      hotseat.setPadding(
          edgeMarginPx + padding.left, 0, edgeMarginPx + padding.right, 2 * edgeMarginPx);
    } else {
      // For phones, layout the hotseat without any bottom margin
      // to ensure that we have space for the folders
      lp.gravity = Gravity.BOTTOM;
      lp.width = LayoutParams.MATCH_PARENT;
      lp.height = hotseatBarHeightPx;
      hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0, 2 * edgeMarginPx, 0);
    }
    hotseat.setLayoutParams(lp);

    // Layout the page indicators
    View pageIndicator = launcher.findViewById(R.id.page_indicator);
    if (pageIndicator != null) {
      if (hasVerticalBarLayout) {
        // Hide the page indicators when we have vertical search/hotseat
        pageIndicator.setVisibility(View.GONE);
      } else {
        // Put the page indicators above the hotseat
        lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
        lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
        lp.width = LayoutParams.WRAP_CONTENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        lp.bottomMargin = hotseatBarHeightPx;
        pageIndicator.setLayoutParams(lp);
      }
    }

    // Layout AllApps
    AppsCustomizeTabHost host =
        (AppsCustomizeTabHost) launcher.findViewById(R.id.apps_customize_pane);
    if (host != null) {
      // Center the all apps page indicator
      int pageIndicatorHeight =
          (int)
              (pageIndicatorHeightPx
                  * Math.min(1f, (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX)));
      pageIndicator = host.findViewById(R.id.apps_customize_page_indicator);
      if (pageIndicator != null) {
        LinearLayout.LayoutParams lllp =
            (LinearLayout.LayoutParams) pageIndicator.getLayoutParams();
        lllp.width = LayoutParams.WRAP_CONTENT;
        lllp.height = pageIndicatorHeight;
        pageIndicator.setLayoutParams(lllp);
      }

      AppsCustomizePagedView pagedView =
          (AppsCustomizePagedView) host.findViewById(R.id.apps_customize_pane_content);

      FrameLayout fakePageContainer = (FrameLayout) host.findViewById(R.id.fake_page_container);
      FrameLayout fakePage = (FrameLayout) host.findViewById(R.id.fake_page);

      padding = new Rect();
      if (pagedView != null) {
        // Constrain the dimensions of all apps so that it does not span the full width
        int paddingLR =
            (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) / (2 * (allAppsNumCols + 1));
        int paddingTB =
            (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows))
                / (2 * (allAppsNumRows + 1));
        paddingLR = Math.min(paddingLR, (int) ((paddingLR + paddingTB) * 0.75f));
        paddingTB = Math.min(paddingTB, (int) ((paddingLR + paddingTB) * 0.75f));
        int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR));
        int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 2;
        // Only adjust the side paddings on landscape phones, or tablets
        if ((isTablet() || isLandscape) && gridPaddingLR > (allAppsCellWidthPx / 4)) {
          padding.left = padding.right = gridPaddingLR;
        }

        // The icons are centered, so we can't just offset by the page indicator height
        // because the empty space will actually be pageIndicatorHeight + paddingTB
        padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB);

        pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight);
        fakePage.setBackground(res.getDrawable(R.drawable.quantum_panel));

        // Horizontal padding for the whole paged view
        int pagedFixedViewPadding =
            res.getDimensionPixelSize(R.dimen.apps_customize_horizontal_padding);

        padding.left += pagedFixedViewPadding;
        padding.right += pagedFixedViewPadding;

        pagedView.setPadding(padding.left, padding.top, padding.right, padding.bottom);
        fakePageContainer.setPadding(padding.left, padding.top, padding.right, padding.bottom);
      }
    }

    // Layout the Overview Mode
    ViewGroup overviewMode = launcher.getOverviewPanel();
    if (overviewMode != null) {
      Rect r = getOverviewModeButtonBarRect();
      lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams();
      lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;

      int visibleChildCount = getVisibleChildCount(overviewMode);
      int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx;
      int maxWidth = totalItemWidth + (visibleChildCount - 1) * overviewModeBarSpacerWidthPx;

      lp.width = Math.min(availableWidthPx, maxWidth);
      lp.height = r.height();
      overviewMode.setLayoutParams(lp);

      if (lp.width > totalItemWidth && visibleChildCount > 1) {
        // We have enough space. Lets add some margin too.
        int margin = (lp.width - totalItemWidth) / (visibleChildCount - 1);
        View lastChild = null;

        // Set margin of all visible children except the last visible child
        for (int i = 0; i < visibleChildCount; i++) {
          if (lastChild != null) {
            MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams();
            if (isLayoutRtl) {
              clp.leftMargin = margin;
            } else {
              clp.rightMargin = margin;
            }
            lastChild.setLayoutParams(clp);
            lastChild = null;
          }
          View thisChild = overviewMode.getChildAt(i);
          if (thisChild.getVisibility() != View.GONE) {
            lastChild = thisChild;
          }
        }
      }
    }
  }