-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLinearLayout.cpp
More file actions
83 lines (71 loc) · 2.24 KB
/
LinearLayout.cpp
File metadata and controls
83 lines (71 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "LinearLayout.hpp"
std::unique_ptr<LayoutParams> LinearLayout::generateDefaultLayoutParams(){
auto lp = std::unique_ptr<LayoutParams>(new LinearLayoutParams(LayoutParams::DimenType(LayoutParams::Params::WRAP_CONTENT),
LayoutParams::DimenType(LayoutParams::Params::WRAP_CONTENT)));
return lp;
}
void LinearLayout::doMeasure(Measure wm, Measure hm){
measureChildren(wm,hm);
int myHt =0, myWt = 0;
for(auto& child : children){
int childWt = child->measuredWt.value;
int childHt = child->measuredHt.value;
if(!orientation){
myWt = std::max(childWt, myWt);
myHt += childHt;
} else {
myHt = std::max(childHt, myHt);
myWt += childWt;
}
}
myWt += mLeftPad + mRightPad;
myHt += mTopPad + mBottomPad;
if(children.size() > 0){
if(!orientation)
myHt += margin*(children.size() - 1);
else
myWt += margin*(children.size() - 1);
}
measuredWt.value = resolveSize(myWt, wm);
measuredHt.value = resolveSize(myHt, hm);
}
void LinearLayout::doLayout(bool, int, int,int,int){
int currTop = mTopPad;
int currLeft = mLeftPad;
for(auto& child : children){
int childL = currLeft;
int childT = currTop;
auto lp = static_cast<LinearLayoutParams*>(child->getLayoutParams());
switch(lp->alignment){
case(Align::START):
break;
case(Align::END):
childL = right - left - mRightPad - child->measuredWt.value;
break;
case(Align::CENTER):
childL = (right - left)/2 - child->measuredWt.value/2;
break;
}
child->layout(childL, childT,
childL + child->measuredWt.value,
childT + child->measuredHt.value);
if(!orientation)
currTop += child->measuredHt.value + margin;
else
currLeft += child->measuredWt.value + margin;
}
}
std::unique_ptr<LinearLayout> LinearLayout::make(){
auto lay = std::make_unique<LinearLayout>();
return std::move(lay);
}
void LinearLayoutParams::set(ViewPtr v, Align align){
auto l = static_cast<LinearLayoutParams*>(v->getLayoutParams());
l->alignment = align;
}
void LinearLayout::add(ViewPtr v, Align align){
auto lp = generateDefaultLayoutParams();
auto lpp = static_cast<LinearLayoutParams*>(lp.get());
lpp->alignment = align;
ViewGroup::addView(v, -1, std::move(lp));
}