Android Open Source - RZAndroidBaseUtils Even Linear Layout






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.raizlabs.widget;
//from   w ww .  j av  a2s .c  om
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

/**
 * {@link LinearLayout} implementation which evenly spaces its children to fit
 * its bounds.
 * @author Dylan James
 *
 */
public class EvenLinearLayout extends LinearLayout {

  public EvenLinearLayout(Context context) {
    super(context);
  }

  public EvenLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (getOrientation() == VERTICAL) {
      layoutVertical();
    } else {
      layoutHorizontal();
    }
  }

  void layoutVertical() {
    final int paddingLeft = getPaddingLeft();
    /** Height after padding */
    final int totalHeight = getHeight() - getPaddingTop() - getPaddingBottom();
    /** The top to set for the children. This will be incremented as we go. */
    int childTop = getPaddingTop();

    /** Total width of the view */
    final int width = getRight() - getLeft();
    /** The right edge of the usable space */
    int rightEdge = width - getPaddingRight();

    /** How much space we have for all of the children */
    int totalChildSpace = rightEdge - paddingLeft;

    final int childCount = getChildCount();
    /** Sum of the heights of the children */
    int totalChildHeight = 0;
    for (int i = 0; i < childCount; ++i) {
      totalChildHeight += getChildAt(i).getMeasuredHeight();
    }

    /** How much extra space each child should be assigned */
    final float marginPerChild = (float)(totalHeight - totalChildHeight) / (float)(childCount + 1);
    
    int childLeft;
    int childWidth;

    View currChild;
    int currTop;
    for (int i = 0; i < childCount; ++i) {
      currChild = getChildAt(i);
      currTop = (int) (childTop + marginPerChild);
      childWidth = currChild.getMeasuredWidth();

      final LayoutParams lp = (LayoutParams) currChild.getLayoutParams();

      // Align the left side using the specified gravity. We're aligning
      // vertically ourselves, so we don't need to respect vertical settings.
      switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
      case Gravity.LEFT:
        childLeft = paddingLeft + lp.leftMargin;
        break;
      case Gravity.CENTER_HORIZONTAL:
        childLeft = paddingLeft + ((totalChildSpace - childWidth) / 2)
            + lp.leftMargin - lp.rightMargin;
        break;
      case Gravity.RIGHT:
        childLeft = rightEdge - childWidth - lp.rightMargin;
        break;
      default:
        childLeft = paddingLeft;
        break;
      }

      // Layout the child
      currChild.layout(childLeft, currTop, childLeft + childWidth, currTop + currChild.getMeasuredHeight());
      // Update the top
      childTop = currTop + currChild.getMeasuredHeight();
    }
  }

  void layoutHorizontal() {
    final int paddingTop = getPaddingTop();
    /** Width after padding */
    final int totalWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    /** The left to set for the children. This will be incremented as we go. */
    int childLeft = getPaddingLeft();

    /** Total height of the view */
    final int height = getBottom() - getTop();
    /** The bottom edge of the usable space */
    int bottomEdge = height - getPaddingBottom();

    /** How much space we have for all of the children */
    int totalChildSpace = bottomEdge - paddingTop;

    final int childCount = getChildCount();
    /** Sum of the widths of the children */
    int totalChildWidth = 0;
    for (int i = 0; i < childCount; ++i) {
      totalChildWidth += getChildAt(i).getMeasuredWidth();
    }

    /** How much extra space each child should be assigned */
    final float marginPerChild = (float)(totalWidth - totalChildWidth) / (float)(childCount + 1);

    int childTop;
    int childHeight;

    View currChild;
    int currLeft;
    for (int i = 0; i < childCount; ++i) {
      currChild = getChildAt(i);
      currLeft = (int) (childLeft + marginPerChild);
      childHeight = currChild.getMeasuredHeight();

      final LayoutParams lp = (LayoutParams) currChild.getLayoutParams();

      // Align the top using the specified gravity. We're aligning
      // horizontally ourselves, so we don't need to respect horizontal
      // settings.
      switch (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK) {
      case Gravity.TOP:
        childTop = paddingTop + lp.topMargin;
        break;
      case Gravity.CENTER_VERTICAL:
        childTop = paddingTop + ((totalChildSpace - childHeight) / 2)
            + lp.topMargin - lp.bottomMargin;
        break;
      case Gravity.BOTTOM:
        childTop = bottomEdge - childHeight - lp.bottomMargin;
        break;
      default:
        childTop = paddingTop;
        break;
      }

      // Layout the child
      currChild.layout(currLeft, childTop, currLeft + currChild.getMeasuredWidth(), childTop + childHeight);
      // Update the left
      childLeft = currLeft + currChild.getMeasuredWidth();
    }
  }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java