Exemplo n.º 1
0
  @Override
  protected void measureChildWithMargins(
      View child,
      int parentWidthMeasureSpec,
      int widthUsed,
      int parentHeightMeasureSpec,
      int heightUsed) {
    final int index = indexOfChild(child);
    final int orientation = getOrientation();
    final LayoutParams params = (LayoutParams) child.getLayoutParams();
    if (hasDividerBeforeChildAt(index)) {
      if (orientation == VERTICAL) {
        // Account for the divider by pushing everything up
        params.topMargin = mDividerHeight;
      } else {
        // Account for the divider by pushing everything left
        params.leftMargin = mDividerWidth;
      }
    }

    final int count = getChildCount();
    if (index == count - 1) {
      if (hasDividerBeforeChildAt(count)) {
        if (orientation == VERTICAL) {
          params.bottomMargin = mDividerHeight;
        } else {
          params.rightMargin = mDividerWidth;
        }
      }
    }
    super.measureChildWithMargins(
        child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
  }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // If we've been given an exact size to match, apply special formatting during layout.
    final boolean wasFormatted = mFormatItems;
    mFormatItems = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY;

    if (wasFormatted != mFormatItems) {
      mFormatItemsWidth = 0; // Reset this when switching modes
    }

    // Special formatting can change whether items can fit as action buttons.
    // Kick the menu and update presenters when this changes.
    final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    if (mFormatItems && mMenu != null && widthSize != mFormatItemsWidth) {
      mFormatItemsWidth = widthSize;
      mMenu.onItemsChanged(true);
    }

    final int childCount = getChildCount();
    if (mFormatItems && childCount > 0) {
      onMeasureExactFormat(widthMeasureSpec, heightMeasureSpec);
    } else {
      // Previous measurement at exact format may have set margins - reset them.
      for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        lp.leftMargin = lp.rightMargin = 0;
      }
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
  }
Exemplo n.º 3
0
  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    menu = findViewById(menuId);
    if (menu == null) {
      throw new RuntimeException();
    }
    rightLayout = findViewById(rightLayoutId);
    if (rightLayout == null) {
      throw new RuntimeException();
    }

    removeView(menu);
    removeView(rightLayout);

    addView(menu);
    addView(rightLayout);

    touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

    // 动态设置左边menu的右边边框距离
    final float scale = myContext.getResources().getDisplayMetrics().density;
    LayoutParams params = (LayoutParams) menu.getLayoutParams();
    params.rightMargin = (int) (FINAL_DP * scale + 0.5f);
    MyLog.e(TAG, "params.rightMargin=" + params.rightMargin);
    menu.setLayoutParams(params);

    initView();
  }
Exemplo n.º 4
0
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   boolean z;
   boolean wasFormatted = this.mFormatItems;
   if (MeasureSpec.getMode(widthMeasureSpec) == 1073741824) {
     z = true;
   } else {
     z = false;
   }
   this.mFormatItems = z;
   if (wasFormatted != this.mFormatItems) {
     this.mFormatItemsWidth = 0;
   }
   int widthSize = MeasureSpec.getSize(widthMeasureSpec);
   if (!(!this.mFormatItems || this.mMenu == null || widthSize == this.mFormatItemsWidth)) {
     this.mFormatItemsWidth = widthSize;
     this.mMenu.onItemsChanged(true);
   }
   int childCount = getChildCount();
   if (!this.mFormatItems || childCount <= 0) {
     for (int i = 0; i < childCount; i++) {
       LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams();
       lp.rightMargin = 0;
       lp.leftMargin = 0;
     }
     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     return;
   }
   onMeasureExactFormat(widthMeasureSpec, heightMeasureSpec);
 }
  private void init() {
    setOrientation(LinearLayout.VERTICAL);

    textView = new TextView(getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
    textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    textView.setTextColor(0xff3b84c0);
    addView(textView);
    LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
    layoutParams.width = LayoutParams.WRAP_CONTENT;
    layoutParams.height = LayoutParams.WRAP_CONTENT;
    layoutParams.leftMargin = AndroidUtilities.dp(8);
    layoutParams.rightMargin = AndroidUtilities.dp(8);
    layoutParams.topMargin = AndroidUtilities.dp(6);
    layoutParams.bottomMargin = AndroidUtilities.dp(4);
    if (LocaleController.isRTL) {
      textView.setGravity(Gravity.RIGHT);
      layoutParams.gravity = Gravity.RIGHT;
    }
    textView.setLayoutParams(layoutParams);

    View view = new View(getContext());
    view.setBackgroundColor(0xff6caae4);
    addView(view);
    layoutParams = (LayoutParams) view.getLayoutParams();
    layoutParams.weight = LayoutParams.MATCH_PARENT;
    layoutParams.height = AndroidUtilities.dp(1);
    view.setLayoutParams(layoutParams);
  }
Exemplo n.º 6
0
  private void setUpCardView() {
    LayoutParams params = new LayoutParams(mCardView.getLayoutParams());
    params.topMargin = mMarginTop;
    params.bottomMargin = mMarginBottom;
    params.leftMargin = mMarginLeft;
    params.rightMargin = mMarginRight;

    mCardView.setLayoutParams(params);
  }
Exemplo n.º 7
0
  /**
   * Show this SnackBar. Make sure it already attached to a parent view or this method will do
   * nothing.
   */
  public void show() {
    ViewGroup parent = (ViewGroup) getParent();
    if (parent == null || mState == STATE_SHOWING || mState == STATE_DISMISSING) return;

    if (parent instanceof FrameLayout) {
      LayoutParams params = (LayoutParams) getLayoutParams();

      params.width = mWidth;
      params.height = mHeight;
      params.gravity = Gravity.START | Gravity.BOTTOM;
      if (mIsRtl) params.rightMargin = mMarginStart;
      else params.leftMargin = mMarginStart;
      params.bottomMargin = mMarginBottom;
    } else if (parent instanceof RelativeLayout) {
      RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();

      params.width = mWidth;
      params.height = mHeight;
      params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
      params.addRule(RelativeLayout.ALIGN_PARENT_START);
      if (mIsRtl) params.rightMargin = mMarginStart;
      else params.leftMargin = mMarginStart;
      params.bottomMargin = mMarginBottom;
    }

    if (mInAnimationId != 0 && mState != STATE_SHOWN) {
      Animation anim = AnimationUtils.loadAnimation(getContext(), mInAnimationId);
      anim.setAnimationListener(
          new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
              setState(STATE_SHOWING);
              setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
              setState(STATE_SHOWN);
              startTimer();
            }
          });
      startAnimation(anim);
    } else {
      setVisibility(View.VISIBLE);
      setState(STATE_SHOWN);
      startTimer();
    }
  }
 private void applyMargins() {
   final int N = getChildCount();
   for (int i = 0; i < N; i++) {
     View child = getChildAt(i);
     if (child.getLayoutParams() instanceof LayoutParams) {
       LayoutParams lp = (LayoutParams) child.getLayoutParams();
       if (!lp.ignoreRightInset && lp.rightMargin != mRightInset) {
         lp.rightMargin = mRightInset;
         child.requestLayout();
       }
     }
   }
 }
Exemplo n.º 9
0
 @Override
 public boolean onResourceReady(
     GlideDrawable resource,
     Object model,
     Target<GlideDrawable> target,
     boolean isFromMemoryCache,
     boolean isFirstResource) {
   if (resource instanceof GlideBitmapDrawable) {
     Log.w(TAG, "onResourceReady() for a Bitmap. Saving.");
     part.setThumbnail(((GlideBitmapDrawable) resource).getBitmap());
   }
   LayoutParams layoutParams = (LayoutParams) getRemoveButton().getLayoutParams();
   if (resource.getIntrinsicWidth() < getWidth()) {
     layoutParams.topMargin = 0;
     layoutParams.rightMargin =
         Math.max(0, (getWidth() - image.getPaddingRight() - resource.getIntrinsicWidth()) / 2);
   } else {
     layoutParams.topMargin =
         Math.max(0, (getHeight() - image.getPaddingTop() - resource.getIntrinsicHeight()) / 2);
     layoutParams.rightMargin = 0;
   }
   getRemoveButton().setLayoutParams(layoutParams);
   return false;
 }
Exemplo n.º 10
0
  private void init(Context context, AttributeSet attrs) {
    mListView = new InternalListView(getContext(), attrs);
    LayoutParams listParams =
        new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    listParams.addRule(ALIGN_PARENT_TOP);
    mListView.setLayoutParams(listParams);
    mListView.setOnScrollListener(new HeaderListViewOnScrollListener());
    mListView.setVerticalScrollBarEnabled(false);
    mListView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mAdapter != null) {
              mAdapter.onItemClick(parent, view, position, id);
            }
          }
        });
    addView(mListView);

    mHeader = new RelativeLayout(getContext());
    LayoutParams headerParams =
        new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    headerParams.addRule(ALIGN_PARENT_TOP);
    mHeader.setLayoutParams(headerParams);
    mHeader.setGravity(Gravity.BOTTOM);
    addView(mHeader);

    // The list view's scroll bar can be hidden by the header, so we display our own scroll bar
    // instead
    Drawable scrollBarDrawable =
        getResources().getDrawable(R.drawable.scrollbar_handle_holo_light, null);
    mScrollView = new FrameLayout(getContext());
    LayoutParams scrollParams =
        new LayoutParams(scrollBarDrawable.getIntrinsicWidth(), LayoutParams.MATCH_PARENT);
    scrollParams.addRule(ALIGN_PARENT_RIGHT);
    scrollParams.rightMargin = (int) dpToPx(2);
    mScrollView.setLayoutParams(scrollParams);

    ImageView scrollIndicator = new ImageView(context);
    scrollIndicator.setLayoutParams(
        new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    scrollIndicator.setImageDrawable(scrollBarDrawable);
    scrollIndicator.setScaleType(ScaleType.FIT_XY);
    mScrollView.addView(scrollIndicator);
    mScrollView.setVisibility(INVISIBLE);

    addView(mScrollView);
  }
Exemplo n.º 11
0
  @Override
  protected void onMeasure(int wSp, int hSp) {
    mMenuView = getChildAt(0);

    if (mAlwaysOpened) {
      View main = getChildAt(1);

      if (mMenuView != null && main != null) {
        measureChild(mMenuView, wSp, hSp);
        LayoutParams lp = (LayoutParams) main.getLayoutParams();

        if (mDirection == DIRECTION_LEFT) {
          lp.leftMargin = mMenuView.getMeasuredWidth();
        } else {
          lp.rightMargin = mMenuView.getMeasuredWidth();
        }
      }
    }

    super.onMeasure(wSp, hSp);
  }
  private void onMeasureExactFormat(int widthMeasureSpec, int heightMeasureSpec) {
    // We already know the width mode is EXACTLY if we're here.
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    final int widthPadding = getPaddingLeft() + getPaddingRight();
    final int heightPadding = getPaddingTop() + getPaddingBottom();

    final int itemHeightSpec =
        getChildMeasureSpec(heightMeasureSpec, heightPadding, ViewGroup.LayoutParams.WRAP_CONTENT);

    widthSize -= widthPadding;

    // Divide the view into cells.
    final int cellCount = widthSize / mMinCellSize;
    final int cellSizeRemaining = widthSize % mMinCellSize;

    if (cellCount == 0) {
      // Give up, nothing fits.
      setMeasuredDimension(widthSize, 0);
      return;
    }

    final int cellSize = mMinCellSize + cellSizeRemaining / cellCount;

    int cellsRemaining = cellCount;
    int maxChildHeight = 0;
    int maxCellsUsed = 0;
    int expandableItemCount = 0;
    int visibleItemCount = 0;
    boolean hasOverflow = false;

    // This is used as a bitfield to locate the smallest items present. Assumes childCount < 64.
    long smallestItemsAt = 0;

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
      final View child = getChildAt(i);
      if (child.getVisibility() == GONE) continue;

      final boolean isGeneratedItem = child instanceof ActionMenuItemView;
      visibleItemCount++;

      if (isGeneratedItem) {
        // Reset padding for generated menu item views; it may change below
        // and views are recycled.
        child.setPadding(mGeneratedItemPadding, 0, mGeneratedItemPadding, 0);
      }

      final LayoutParams lp = (LayoutParams) child.getLayoutParams();
      lp.expanded = false;
      lp.extraPixels = 0;
      lp.cellsUsed = 0;
      lp.expandable = false;
      lp.leftMargin = 0;
      lp.rightMargin = 0;
      lp.preventEdgeOffset = isGeneratedItem && ((ActionMenuItemView) child).hasText();

      // Overflow always gets 1 cell. No more, no less.
      final int cellsAvailable = lp.isOverflowButton ? 1 : cellsRemaining;

      final int cellsUsed =
          measureChildForCells(child, cellSize, cellsAvailable, itemHeightSpec, heightPadding);

      maxCellsUsed = Math.max(maxCellsUsed, cellsUsed);
      if (lp.expandable) expandableItemCount++;
      if (lp.isOverflowButton) hasOverflow = true;

      cellsRemaining -= cellsUsed;
      maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
      if (cellsUsed == 1) smallestItemsAt |= (1 << i);
    }

    // When we have overflow and a single expanded (text) item, we want to try centering it
    // visually in the available space even though overflow consumes some of it.
    final boolean centerSingleExpandedItem = hasOverflow && visibleItemCount == 2;

    // Divide space for remaining cells if we have items that can expand.
    // Try distributing whole leftover cells to smaller items first.

    boolean needsExpansion = false;
    while (expandableItemCount > 0 && cellsRemaining > 0) {
      int minCells = Integer.MAX_VALUE;
      long minCellsAt = 0; // Bit locations are indices of relevant child views
      int minCellsItemCount = 0;
      for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        // Don't try to expand items that shouldn't.
        if (!lp.expandable) continue;

        // Mark indices of children that can receive an extra cell.
        if (lp.cellsUsed < minCells) {
          minCells = lp.cellsUsed;
          minCellsAt = 1 << i;
          minCellsItemCount = 1;
        } else if (lp.cellsUsed == minCells) {
          minCellsAt |= 1 << i;
          minCellsItemCount++;
        }
      }

      // Items that get expanded will always be in the set of smallest items when we're done.
      smallestItemsAt |= minCellsAt;

      if (minCellsItemCount > cellsRemaining) break; // Couldn't expand anything evenly. Stop.

      // We have enough cells, all minimum size items will be incremented.
      minCells++;

      for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if ((minCellsAt & (1 << i)) == 0) {
          // If this item is already at our small item count, mark it for later.
          if (lp.cellsUsed == minCells) smallestItemsAt |= 1 << i;
          continue;
        }

        if (centerSingleExpandedItem && lp.preventEdgeOffset && cellsRemaining == 1) {
          // Add padding to this item such that it centers.
          child.setPadding(mGeneratedItemPadding + cellSize, 0, mGeneratedItemPadding, 0);
        }
        lp.cellsUsed++;
        lp.expanded = true;
        cellsRemaining--;
      }

      needsExpansion = true;
    }

    // Divide any space left that wouldn't divide along cell boundaries
    // evenly among the smallest items

    final boolean singleItem = !hasOverflow && visibleItemCount == 1;
    if (cellsRemaining > 0
        && smallestItemsAt != 0
        && (cellsRemaining < visibleItemCount - 1 || singleItem || maxCellsUsed > 1)) {
      float expandCount = Long.bitCount(smallestItemsAt);

      if (!singleItem) {
        // The items at the far edges may only expand by half in order to pin to either side.
        if ((smallestItemsAt & 1) != 0) {
          LayoutParams lp = (LayoutParams) getChildAt(0).getLayoutParams();
          if (!lp.preventEdgeOffset) expandCount -= 0.5f;
        }
        if ((smallestItemsAt & (1 << (childCount - 1))) != 0) {
          LayoutParams lp = ((LayoutParams) getChildAt(childCount - 1).getLayoutParams());
          if (!lp.preventEdgeOffset) expandCount -= 0.5f;
        }
      }

      final int extraPixels = expandCount > 0 ? (int) (cellsRemaining * cellSize / expandCount) : 0;

      for (int i = 0; i < childCount; i++) {
        if ((smallestItemsAt & (1 << i)) == 0) continue;

        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (child instanceof ActionMenuItemView) {
          // If this is one of our views, expand and measure at the larger size.
          lp.extraPixels = extraPixels;
          lp.expanded = true;
          if (i == 0 && !lp.preventEdgeOffset) {
            // First item gets part of its new padding pushed out of sight.
            // The last item will get this implicitly from layout.
            lp.leftMargin = -extraPixels / 2;
          }
          needsExpansion = true;
        } else if (lp.isOverflowButton) {
          lp.extraPixels = extraPixels;
          lp.expanded = true;
          lp.rightMargin = -extraPixels / 2;
          needsExpansion = true;
        } else {
          // If we don't know what it is, give it some margins instead
          // and let it center within its space. We still want to pin
          // against the edges.
          if (i != 0) {
            lp.leftMargin = extraPixels / 2;
          }
          if (i != childCount - 1) {
            lp.rightMargin = extraPixels / 2;
          }
        }
      }

      cellsRemaining = 0;
    }

    // Remeasure any items that have had extra space allocated to them.
    if (needsExpansion) {
      for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        if (!lp.expanded) continue;

        final int width = lp.cellsUsed * cellSize + lp.extraPixels;
        child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), itemHeightSpec);
      }
    }

    if (heightMode != MeasureSpec.EXACTLY) {
      heightSize = maxChildHeight;
    }

    setMeasuredDimension(widthSize, heightSize);
  }
Exemplo n.º 13
0
 private void onMeasureExactFormat(int widthMeasureSpec, int heightMeasureSpec) {
   int heightMode = MeasureSpec.getMode(heightMeasureSpec);
   int widthSize = MeasureSpec.getSize(widthMeasureSpec);
   int heightSize = MeasureSpec.getSize(heightMeasureSpec);
   int widthPadding = getPaddingLeft() + getPaddingRight();
   int heightPadding = getPaddingTop() + getPaddingBottom();
   int itemHeightSpec = getChildMeasureSpec(heightMeasureSpec, heightPadding, -2);
   widthSize -= widthPadding;
   int cellCount = widthSize / this.mMinCellSize;
   int cellSizeRemaining = widthSize % this.mMinCellSize;
   if (cellCount == 0) {
     setMeasuredDimension(widthSize, 0);
     return;
   }
   int i;
   LayoutParams lp;
   int cellSize = this.mMinCellSize + (cellSizeRemaining / cellCount);
   int cellsRemaining = cellCount;
   int maxChildHeight = 0;
   int maxCellsUsed = 0;
   int expandableItemCount = 0;
   int visibleItemCount = 0;
   boolean hasOverflow = false;
   long smallestItemsAt = 0;
   int childCount = getChildCount();
   for (i = 0; i < childCount; i++) {
     View child = getChildAt(i);
     if (child.getVisibility() != 8) {
       int cellsAvailable;
       boolean isGeneratedItem = child instanceof ActionMenuItemView;
       visibleItemCount++;
       if (isGeneratedItem) {
         child.setPadding(this.mGeneratedItemPadding, 0, this.mGeneratedItemPadding, 0);
       }
       lp = (LayoutParams) child.getLayoutParams();
       lp.expanded = false;
       lp.extraPixels = 0;
       lp.cellsUsed = 0;
       lp.expandable = false;
       lp.leftMargin = 0;
       lp.rightMargin = 0;
       boolean z = isGeneratedItem && ((ActionMenuItemView) child).hasText();
       lp.preventEdgeOffset = z;
       if (lp.isOverflowButton) {
         cellsAvailable = 1;
       } else {
         cellsAvailable = cellsRemaining;
       }
       int cellsUsed =
           measureChildForCells(child, cellSize, cellsAvailable, itemHeightSpec, heightPadding);
       maxCellsUsed = Math.max(maxCellsUsed, cellsUsed);
       if (lp.expandable) {
         expandableItemCount++;
       }
       if (lp.isOverflowButton) {
         hasOverflow = true;
       }
       cellsRemaining -= cellsUsed;
       maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight());
       if (cellsUsed == 1) {
         smallestItemsAt |= (long) (1 << i);
       }
     }
   }
   boolean centerSingleExpandedItem = hasOverflow && visibleItemCount == 2;
   boolean needsExpansion = false;
   while (expandableItemCount > 0 && cellsRemaining > 0) {
     int minCells = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
     long minCellsAt = 0;
     int minCellsItemCount = 0;
     for (i = 0; i < childCount; i++) {
       int i2;
       lp = (LayoutParams) getChildAt(i).getLayoutParams();
       if (lp.expandable) {
         i2 = lp.cellsUsed;
         if (r0 < minCells) {
           minCells = lp.cellsUsed;
           minCellsAt = (long) (1 << i);
           minCellsItemCount = 1;
         } else {
           i2 = lp.cellsUsed;
           if (r0 == minCells) {
             minCellsAt |= (long) (1 << i);
             minCellsItemCount++;
           }
         }
       }
     }
     smallestItemsAt |= minCellsAt;
     if (minCellsItemCount > cellsRemaining) {
       break;
     }
     minCells++;
     for (i = 0; i < childCount; i++) {
       child = getChildAt(i);
       lp = (LayoutParams) child.getLayoutParams();
       if ((((long) (1 << i)) & minCellsAt) == 0) {
         i2 = lp.cellsUsed;
         if (r0 == minCells) {
           smallestItemsAt |= (long) (1 << i);
         }
       } else {
         if (centerSingleExpandedItem && lp.preventEdgeOffset && cellsRemaining == 1) {
           child.setPadding(
               this.mGeneratedItemPadding + cellSize, 0, this.mGeneratedItemPadding, 0);
         }
         lp.cellsUsed++;
         lp.expanded = true;
         cellsRemaining--;
       }
     }
     needsExpansion = true;
   }
   boolean singleItem = !hasOverflow && visibleItemCount == 1;
   if (cellsRemaining > 0
       && smallestItemsAt != 0
       && (cellsRemaining < visibleItemCount - 1 || singleItem || maxCellsUsed > 1)) {
     float expandCount = (float) Long.bitCount(smallestItemsAt);
     if (!singleItem) {
       if ((1 & smallestItemsAt) != 0) {
         if (!((LayoutParams) getChildAt(0).getLayoutParams()).preventEdgeOffset) {
           expandCount -= 0.5f;
         }
       }
       if ((((long) (1 << (childCount - 1))) & smallestItemsAt) != 0) {
         if (!((LayoutParams) getChildAt(childCount - 1).getLayoutParams()).preventEdgeOffset) {
           expandCount -= 0.5f;
         }
       }
     }
     int extraPixels =
         expandCount > 0.0f ? (int) (((float) (cellsRemaining * cellSize)) / expandCount) : 0;
     for (i = 0; i < childCount; i++) {
       if ((((long) (1 << i)) & smallestItemsAt) != 0) {
         child = getChildAt(i);
         lp = (LayoutParams) child.getLayoutParams();
         if (child instanceof ActionMenuItemView) {
           lp.extraPixels = extraPixels;
           lp.expanded = true;
           if (i == 0 && !lp.preventEdgeOffset) {
             lp.leftMargin = (-extraPixels) / 2;
           }
           needsExpansion = true;
         } else if (lp.isOverflowButton) {
           lp.extraPixels = extraPixels;
           lp.expanded = true;
           lp.rightMargin = (-extraPixels) / 2;
           needsExpansion = true;
         } else {
           if (i != 0) {
             lp.leftMargin = extraPixels / 2;
           }
           if (i != childCount - 1) {
             lp.rightMargin = extraPixels / 2;
           }
         }
       }
     }
   }
   if (needsExpansion) {
     for (i = 0; i < childCount; i++) {
       child = getChildAt(i);
       lp = (LayoutParams) child.getLayoutParams();
       if (lp.expanded) {
         child.measure(
             MeasureSpec.makeMeasureSpec((lp.cellsUsed * cellSize) + lp.extraPixels, 1073741824),
             itemHeightSpec);
       }
     }
   }
   if (heightMode != 1073741824) {
     heightSize = maxChildHeight;
   }
   setMeasuredDimension(widthSize, heightSize);
 }
Exemplo n.º 14
0
 public void setMarginRightCheckBox(int marginRight) {
   LayoutParams params = (LayoutParams) checkOption.getLayoutParams();
   params.rightMargin = marginRight;
   checkOption.setLayoutParams(params);
 }
  private void init() {
    calendarGesture = new GestureDetector(context, new GestureListener());
    cal = Calendar.getInstance();
    base = new RelativeLayout(context);
    base.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    base.setMinimumHeight(40);

    base.setId(baseId);

    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.leftMargin = 16;
    params.topMargin = 40;
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    prev = new ImageView(context);
    prev.setId(preId);
    prev.setLayoutParams(params);
    prev.setImageResource(R.drawable.navigation_previous_item);
    prev.setOnClickListener(this);
    base.addView(prev);

    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    month = new TextView(context);
    month.setId(monthId);
    month.setLayoutParams(params);
    //        month.setTextAppearance(context, android.R.attr.textAppearanceLarge);
    month.setText(
        cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault())
            + " "
            + cal.get(Calendar.YEAR));
    month.setTextSize(25);

    base.addView(month);

    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.rightMargin = 16;
    params.topMargin = 40;
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    next = new ImageView(context);
    next.setImageResource(R.drawable.navigation_next_item);
    next.setLayoutParams(params);
    next.setId(nextId);
    next.setOnClickListener(this);

    base.addView(next);

    addView(base);

    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.bottomMargin = 20;
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.addRule(RelativeLayout.BELOW, base.getId());

    calendar = new GridView(context);
    calendar.setLayoutParams(params);
    calendar.setVerticalSpacing(4);
    calendar.setHorizontalSpacing(4);
    calendar.setNumColumns(7);
    calendar.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
    calendar.setDrawSelectorOnTop(true);

    mAdapter = new CalendarAdapter(context, cal);
    calendar.setAdapter(mAdapter);
    calendar.setOnTouchListener(
        new OnTouchListener() {

          @Override
          public boolean onTouch(View v, MotionEvent event) {
            return calendarGesture.onTouchEvent(event);
          }
        });

    addView(calendar);
  }
Exemplo n.º 16
0
  @SuppressWarnings("static-access")
  private void init() {

    this.setBackgroundResource(R.drawable.cs_recommend_bg);
    this.setOrientation(LinearLayout.VERTICAL);
    RelativeLayout contentLayout = new RelativeLayout(context);
    LayoutParams layoutParams =
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    contentLayout.setLayoutParams(layoutParams);
    this.addView(contentLayout);

    TextView titleTextView = new TextView(context);
    titleTextView.setText(R.string.download_manager);
    titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize + 3);
    titleTextView.setTextColor(Color.WHITE);
    int titleMargin = 20;
    RelativeLayout.LayoutParams titleParams =
        new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    titleParams.leftMargin = titleMargin;
    titleParams.rightMargin = titleMargin;
    titleParams.topMargin = titleMargin;
    titleParams.bottomMargin = titleMargin;
    titleTextView.setLayoutParams(titleParams);
    contentLayout.addView(titleTextView);

    LinearLayout managerLayout = new LinearLayout(context);
    managerLayout.setId(MANAGERLAYOUT_ID);
    managerLayout.setOrientation(LinearLayout.VERTICAL);
    int managerPad = 10;
    managerLayout.setPadding(managerPad, managerPad, managerPad, managerPad);
    int height = DisplayManager.getInstance(context).getScreenHeight() - 150;
    int width = DisplayManager.getInstance(context).getScreenWidth() - 200;
    RelativeLayout.LayoutParams managerLayoutParams =
        new RelativeLayout.LayoutParams(width, height);
    managerLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    managerLayout.setLayoutParams(managerLayoutParams);
    managerLayout.setBackgroundResource(R.drawable.bg_downmanger_content);
    // managerLayout.setBackgroundColor(Color.parseColor("#99000000"));
    contentLayout.addView(managerLayout);

    LinearLayout managerPathLayout = new LinearLayout(context);
    managerPathLayout.setOrientation(LinearLayout.HORIZONTAL);
    LayoutParams managerPathParams =
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, textSize + 50);
    managerPathLayout.setLayoutParams(managerPathParams);
    managerPathLayout.setPadding(managerPad, 0, managerPad, managerPad);

    TextView pathText = new TextView(context);
    pathText.setText(R.string.present_path);
    pathText.setGravity(Gravity.CENTER);
    pathText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize - 3);
    LayoutParams pathTextParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3);
    pathText.setTextColor(Color.parseColor("#ffffff"));
    pathTextParams.setMargins(0, 0, 0, 0);
    pathTextParams.gravity = Gravity.CENTER;
    pathText.setLayoutParams(pathTextParams);
    managerPathLayout.addView(pathText);

    final CheckDownDialogView checkDialog = new CheckDownDialogView(context);
    String present_edit = "";
    if (checkDialog.DetectionDisk() != null) {
      present_edit = checkDialog.getDownPathname();
    }
    pathEdit = new EditText(context);
    pathEdit.setText(present_edit);
    pathEdit.setFocusable(false);
    pathEdit.setGravity(Gravity.CENTER_VERTICAL);
    pathEdit.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize - 3);
    LayoutParams pathEditParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 12);
    pathEdit.setTextColor(Color.parseColor("#ffffff"));
    pathEdit.setBackgroundResource(R.drawable.present_path);
    pathEditParams.gravity = Gravity.CENTER_VERTICAL;
    pathEdit.setLayoutParams(pathEditParams);
    managerPathLayout.addView(pathEdit);

    Button pathButton = new FocusAbleButton(context);
    pathButton.setText(R.string.present_sele);
    pathButton.setTextColor(Color.parseColor("#ffffff"));
    pathButton.setGravity(Gravity.CENTER);
    pathButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize - 3);
    LayoutParams pathButtonViewParams =
        new LinearLayout.LayoutParams(0, layoutParams.WRAP_CONTENT, 2);
    pathButtonViewParams.gravity = Gravity.CENTER;
    pathButtonViewParams.setMargins(35, 0, 0, 0);
    pathButton.setLayoutParams(pathButtonViewParams);
    managerPathLayout.addView(pathButton);
    pathButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            //				Map<String, String> labMap = checkDialog.DetectionDisk();
            List<String> pathsStr = DownloadResourceManager.getInstance().getFileDir();
            if (pathsStr != null) {
              checkDialog.choiceUsbDialog();
              checkDialog.setListener(
                  new PathChoiceClickListener() {
                    @Override
                    public void onClick(View v) {
                      // 下载进度保存
                      LinkedHashMap<String, FilmDownLoad4k> downStatus =
                          DownloadResourceManager.getInstance().getDLStatus();
                      if (downStatus != null) {
                        DownloadResourceManager.getInstance().writeDownStatus(downStatus);
                      }
                      DownloadResourceManager.getInstance().clearDLData();
                      String path = (String) v.getTag();
                      DownloadResourceManager.getInstance().initDownPath(path);
                      // Log.e(dlStatus +" "+dlStatus.size());
                      if (context instanceof DownManagerActivity) {
                        ((DownManagerActivity) context).setData(true);
                      }

                      String present_edit = checkDialog.getDownPathname();
                      pathEdit.setText(present_edit);

                      // 初始化新路径后  检测U盘下载 更新UI
                      //							LinkedHashMap<String, FilmDownLoad4k> downStatu =
                      // DownloadResourceManager.getInstance().getDLStatus();
                      //							String path_dir =
                      // DownloadResourceManager.getInstance().getDownLoadPath();
                      //							if(downStatu != null){
                      //								if((path+"/voole_video").equalsIgnoreCase(path_dir)){

                      //									File downPath = new File(path_dir);
                      //									if (TextUtils.isEmpty(DownloadResourceManager.getInstance()
                      //											.getDownLoadPath())
                      //									|| !downPath.getParentFile().exists() || !downPath.exists()){
                      //										if(clickNum == 0){
                      //											managerAct.updateDownItem();
                      //										}
                      //										clickNum ++;
                      //									}
                      if (context instanceof DownManagerActivity) {
                        if (!((DownManagerActivity) context).isAlive) { // 如果下载线程停止
                          ((DownManagerActivity) context).setData(true); // 存放数据
                          ((DownManagerActivity) context).updateDownItem(); // 启动下载线程
                        }
                      }
                    }
                  });

            } else {
              // 没插入U盘
              TVAlertDialog tvDialog = new TVAlertDialog(context);
              tvDialog.showInspectDialog(context);
            }
          }
        });

    LayoutParams parmsPath = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
    parmsPath.weight = 1;
    parmsPath.topMargin = 20;
    parmsPath.leftMargin = 40;
    parmsPath.rightMargin = 90;
    managerLayout.addView(managerPathLayout, parmsPath);

    LinearLayout managerTitleLayout = new LinearLayout(context);
    managerTitleLayout.setOrientation(LinearLayout.HORIZONTAL);
    managerTitleLayout.setBackgroundResource(R.drawable.bg_down_title);
    LayoutParams managerTitleLayoutParams =
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, textSize + 50);
    managerTitleLayout.setLayoutParams(managerTitleLayoutParams);
    managerTitleLayout.setPadding(managerPad, managerPad - 4, managerPad, managerPad + 4);

    TextView nameText = new TextView(context);
    nameText.setText(R.string.filename);
    nameText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    LayoutParams nameTextParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3);
    nameText.setTextColor(Color.parseColor("#d5ac84"));
    nameText.setGravity(Gravity.CENTER);
    nameText.setLayoutParams(nameTextParams);
    managerTitleLayout.addView(nameText);

    TextView progressText = new TextView(context);
    LayoutParams progressTextParams =
        new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 4);
    progressText.setText(R.string.progress);
    progressText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    progressText.setTextColor(Color.parseColor("#d5ac84"));
    progressText.setGravity(Gravity.CENTER);
    progressText.setLayoutParams(progressTextParams);
    managerTitleLayout.addView(progressText);

    TextView speedText = new TextView(context);
    speedText.setText(R.string.speed);
    speedText.setTextColor(Color.parseColor("#d5ac84"));
    speedText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    LayoutParams speedTextParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
    speedText.setGravity(Gravity.CENTER);
    speedText.setLayoutParams(speedTextParams);
    managerTitleLayout.addView(speedText);

    TextView sizeText = new TextView(context);
    sizeText.setText(R.string.size);
    sizeText.setTextColor(Color.parseColor("#d5ac84"));
    sizeText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    LayoutParams sizeTextParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
    sizeText.setGravity(Gravity.CENTER);
    sizeText.setLayoutParams(sizeTextParams);
    managerTitleLayout.addView(sizeText);

    TextView handleText = new TextView(context);
    handleText.setText(R.string.operation);
    handleText.setTextColor(Color.parseColor("#d5ac84"));
    handleText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    LayoutParams handleTextParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
    handleText.setGravity(Gravity.CENTER);
    handleText.setLayoutParams(handleTextParams);
    managerTitleLayout.addView(handleText);
    LayoutParams parms = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
    parms.weight = 1;
    parms.leftMargin = 30;
    parms.rightMargin = 30;
    parms.topMargin = managerPad - 5;
    managerLayout.addView(managerTitleLayout, parms);

    adapterLinearLayout = new FilmLinearLayout(context);
    LayoutParams adapterLinearLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
    adapterLinearLayoutParams.weight = 6;
    managerLayout.addView(adapterLinearLayout, adapterLinearLayoutParams);
    adapterLinearLayout.setId(FilmLinearLayout.ADAPTERLINEARLAYOUT_ID);

    int pading = 20;
    LinearLayout pageIndexLayout = new LinearLayout(context);
    pageIndexLayout.setOrientation(LinearLayout.HORIZONTAL);
    LayoutParams pageIndexLayoutLayoutParams =
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    pageIndexLayout.setLayoutParams(pageIndexLayoutLayoutParams);
    pageIndexLayoutLayoutParams.setMargins(0, pading, 0, 0);
    pageIndexLayout.setGravity(Gravity.CENTER);

    Button preButton = new FocusAbleButton(context);
    preButton.setText(R.string.pre_page);
    preButton.setTextColor(Color.WHITE);
    preButton.setGravity(Gravity.CENTER);
    preButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    LayoutParams preButtonViewParams =
        new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    preButton.setLayoutParams(preButtonViewParams);
    pageIndexLayout.addView(preButton);
    preButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            adapterLinearLayout.showPrePage();
            pageIndexTextview.setText(
                adapterLinearLayout.getCurrentPage() + "/" + adapterLinearLayout.getTotalPage());
          }
        });
    preButton.setPadding(pading, 0, pading, 0);

    Button nextButton = new FocusAbleButton(context);
    nextButton.setText(R.string.next_page);
    nextButton.setTextColor(Color.WHITE);
    nextButton.setGravity(Gravity.CENTER);
    nextButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    LayoutParams nextButtonViewParams =
        new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    nextButtonViewParams.leftMargin = textSize - 5;
    nextButton.setLayoutParams(nextButtonViewParams);
    pageIndexLayout.addView(nextButton);
    nextButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            adapterLinearLayout.showNextPage();
            pageIndexTextview.setText(
                adapterLinearLayout.getCurrentPage() + "/" + adapterLinearLayout.getTotalPage());
          }
        });
    nextButton.setPadding(pading, 0, pading, 0);

    pageIndexTextview = new TextView(context);
    pageIndexTextview.setText(
        adapterLinearLayout.getCurrentPage() + "/" + adapterLinearLayout.getTotalPage());
    pageIndexTextview.setTextColor(Color.WHITE);
    pageIndexTextview.setGravity(Gravity.CENTER);
    pageIndexTextview.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    LayoutParams pageIndexTextviewParams =
        new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    pageIndexTextviewParams.leftMargin = textSize - 5;
    pageIndexTextview.setLayoutParams(pageIndexTextviewParams);
    pageIndexLayout.addView(pageIndexTextview);
    LayoutParams pageIndexLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
    pageIndexLayoutParams.weight = 1;
    managerLayout.addView(pageIndexLayout, pageIndexLayoutParams);

    TextView warnInfoTextView = new TextView(getContext());
    warnInfoTextView.setText(R.string.down_manager_warn_info);
    warnInfoTextView.setTextColor(Color.WHITE);
    warnInfoTextView.setGravity(Gravity.CENTER);
    warnInfoTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    android.widget.RelativeLayout.LayoutParams warnInfoLayoutParams =
        new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    warnInfoLayoutParams.addRule(RelativeLayout.BELOW, MANAGERLAYOUT_ID);
    warnInfoLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, MANAGERLAYOUT_ID);
    warnInfoLayoutParams.topMargin = textSize - 10;
    contentLayout.addView(warnInfoTextView, warnInfoLayoutParams);
  }