| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.content.res.TypedArray; |
| 21 |
import android.util.AttributeSet; |
| 22 |
import android.view.View; |
| 23 |
import android.view.ViewGroup; |
| 24 |
import android.view.inspector.InspectableProperty; |
| 25 |
import android.widget.RemoteViews.RemoteView; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
@Deprecated |
| 41 |
@RemoteView |
| 42 |
public class AbsoluteLayout extends ViewGroup { |
| 43 |
public AbsoluteLayout(Context context) { |
| 44 |
this(context, null); |
| 45 |
} |
| 46 |
|
| 47 |
public AbsoluteLayout(Context context, AttributeSet attrs) { |
| 48 |
this(context, attrs, 0); |
| 49 |
} |
| 50 |
|
| 51 |
public AbsoluteLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| 52 |
this(context, attrs, defStyleAttr, 0); |
| 53 |
} |
| 54 |
|
| 55 |
public AbsoluteLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 56 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 61 |
int count = getChildCount(); |
| 62 |
|
| 63 |
int maxHeight = 0; |
| 64 |
int maxWidth = 0; |
| 65 |
|
| 66 |
|
| 67 |
measureChildren(widthMeasureSpec, heightMeasureSpec); |
| 68 |
|
| 69 |
|
| 70 |
for (int i = 0; i < count; i++) { |
| 71 |
View child = getChildAt(i); |
| 72 |
if (child.getVisibility() != GONE) { |
| 73 |
int childRight; |
| 74 |
int childBottom; |
| 75 |
|
| 76 |
AbsoluteLayout.LayoutParams lp |
| 77 |
= (AbsoluteLayout.LayoutParams) child.getLayoutParams(); |
| 78 |
|
| 79 |
childRight = lp.x + child.getMeasuredWidth(); |
| 80 |
childBottom = lp.y + child.getMeasuredHeight(); |
| 81 |
|
| 82 |
maxWidth = Math.max(maxWidth, childRight); |
| 83 |
maxHeight = Math.max(maxHeight, childBottom); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
maxWidth += mPaddingLeft + mPaddingRight; |
| 89 |
maxHeight += mPaddingTop + mPaddingBottom; |
| 90 |
|
| 91 |
|
| 92 |
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); |
| 93 |
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); |
| 94 |
|
| 95 |
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0), |
| 96 |
resolveSizeAndState(maxHeight, heightMeasureSpec, 0)); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
@Override |
| 106 |
protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
| 107 |
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
protected void onLayout(boolean changed, int l, int t, |
| 112 |
int r, int b) { |
| 113 |
int count = getChildCount(); |
| 114 |
|
| 115 |
for (int i = 0; i < count; i++) { |
| 116 |
View child = getChildAt(i); |
| 117 |
if (child.getVisibility() != GONE) { |
| 118 |
|
| 119 |
AbsoluteLayout.LayoutParams lp = |
| 120 |
(AbsoluteLayout.LayoutParams) child.getLayoutParams(); |
| 121 |
|
| 122 |
int childLeft = mPaddingLeft + lp.x; |
| 123 |
int childTop = mPaddingTop + lp.y; |
| 124 |
child.layout(childLeft, childTop, |
| 125 |
childLeft + child.getMeasuredWidth(), |
| 126 |
childTop + child.getMeasuredHeight()); |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 134 |
return new AbsoluteLayout.LayoutParams(getContext(), attrs); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
@Override |
| 139 |
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 140 |
return p instanceof AbsoluteLayout.LayoutParams; |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 145 |
return new LayoutParams(p); |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public boolean shouldDelayChildPressedState() { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static class LayoutParams extends ViewGroup.LayoutParams { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
@InspectableProperty(name = "layout_x") |
| 164 |
public int x; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
@InspectableProperty(name = "layout_y") |
| 169 |
public int y; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public LayoutParams(int width, int height, int x, int y) { |
| 183 |
super(width, height); |
| 184 |
this.x = x; |
| 185 |
this.y = y; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public LayoutParams(Context c, AttributeSet attrs) { |
| 205 |
super(c, attrs); |
| 206 |
TypedArray a = c.obtainStyledAttributes(attrs, |
| 207 |
com.android.internal.R.styleable.AbsoluteLayout_Layout); |
| 208 |
x = a.getDimensionPixelOffset( |
| 209 |
com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_x, 0); |
| 210 |
y = a.getDimensionPixelOffset( |
| 211 |
com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_y, 0); |
| 212 |
a.recycle(); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public LayoutParams(ViewGroup.LayoutParams source) { |
| 219 |
super(source); |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public String debug(String output) { |
| 224 |
return output + "Absolute.LayoutParams={width=" |
| 225 |
+ sizeToString(width) + ", height=" + sizeToString(height) |
| 226 |
+ " x=" + x + " y=" + y + "}"; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|