AI2 Component  (Version nb184)
LinearLayout.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
11 
12 import android.content.Context;
13 import android.view.ViewGroup;
14 
19 @SimpleObject
20 public final class LinearLayout implements Layout {
21 
22  private final android.widget.LinearLayout layoutManager;
23 
32  LinearLayout(Context context, int orientation) {
33  this(context, orientation, null, null);
34  }
35 
46  LinearLayout(Context context, int orientation, final Integer preferredEmptyWidth,
47  final Integer preferredEmptyHeight) {
48  if (preferredEmptyWidth == null && preferredEmptyHeight != null ||
49  preferredEmptyWidth != null && preferredEmptyHeight == null) {
50  throw new IllegalArgumentException("LinearLayout - preferredEmptyWidth and " +
51  "preferredEmptyHeight must be either both null or both not null");
52  }
53 
54  // Create an Android LinearLayout, but override onMeasure so that we can use our preferred
55  // empty width/height.
56  layoutManager = new android.widget.LinearLayout(context) {
57  @Override
58  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
59  // If there was no preferred empty width/height specified (see constructors above), just
60  // call super. (This is the case for the Form component.)
61  if (preferredEmptyWidth == null || preferredEmptyHeight == null) {
62  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
63  return;
64  }
65 
66  // If the layout has any children, just call super.
67  if (getChildCount() != 0) {
68  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
69  return;
70  }
71 
72  setMeasuredDimension(getSize(widthMeasureSpec, preferredEmptyWidth),
73  getSize(heightMeasureSpec, preferredEmptyHeight));
74  }
75 
76  private int getSize(int measureSpec, int preferredSize) {
77  int result;
78  int specMode = MeasureSpec.getMode(measureSpec);
79  int specSize = MeasureSpec.getSize(measureSpec);
80 
81  if (specMode == MeasureSpec.EXACTLY) {
82  // We were told how big to be
83  result = specSize;
84  } else {
85  // Use the preferred size.
86  result = preferredSize;
87  if (specMode == MeasureSpec.AT_MOST) {
88  // Respect AT_MOST value if that was what is called for by measureSpec
89  result = Math.min(result, specSize);
90  }
91  }
92 
93  return result;
94  }
95  };
96 
97  layoutManager.setOrientation(
99  android.widget.LinearLayout.HORIZONTAL : android.widget.LinearLayout.VERTICAL);
100  }
101 
102  // Layout implementation
103 
104  public ViewGroup getLayoutManager() {
105  return layoutManager;
106  }
107 
108  public void add(AndroidViewComponent component) {
109  layoutManager.addView(component.getView(), new android.widget.LinearLayout.LayoutParams(
110  ViewGroup.LayoutParams.WRAP_CONTENT, // width
111  ViewGroup.LayoutParams.WRAP_CONTENT, // height
112  0f)); // weight
113  }
114 
115  public void setHorizontalGravity(int gravity) {
116  layoutManager.setHorizontalGravity(gravity);
117  }
118 
119  public void setVerticalGravity(int gravity) {
120  layoutManager.setVerticalGravity(gravity);
121  }
122 
123  public void setBaselineAligned(boolean baselineAligned) { layoutManager.setBaselineAligned(baselineAligned); }
124 
125 }
com.google.appinventor.components.runtime.Layout
Definition: Layout.java:16
com.google.appinventor.components.runtime.LinearLayout.add
void add(AndroidViewComponent component)
Definition: LinearLayout.java:108
com.google.appinventor.components
com.google.appinventor.components.runtime.LinearLayout.setVerticalGravity
void setVerticalGravity(int gravity)
Definition: LinearLayout.java:119
com.google.appinventor.components.runtime.LinearLayout.setBaselineAligned
void setBaselineAligned(boolean baselineAligned)
Definition: LinearLayout.java:123
com.google.appinventor.components.runtime.LinearLayout.getLayoutManager
ViewGroup getLayoutManager()
Definition: LinearLayout.java:104
com.google.appinventor.components.runtime.LinearLayout.setHorizontalGravity
void setHorizontalGravity(int gravity)
Definition: LinearLayout.java:115
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentConstants.LAYOUT_ORIENTATION_HORIZONTAL
static final int LAYOUT_ORIENTATION_HORIZONTAL
Definition: ComponentConstants.java:26
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.AndroidViewComponent.getView
abstract View getView()
com.google
com
com.google.appinventor.components.common.ComponentConstants
Definition: ComponentConstants.java:13
com.google.appinventor.components.runtime.LinearLayout
Definition: LinearLayout.java:20
com.google.appinventor.components.runtime.AndroidViewComponent
Definition: AndroidViewComponent.java:27
com.google.appinventor.components.annotations
com.google.appinventor