xm
2019-07-16 b910cb79c9b5bcc204022a3cf9e6950f0a64dfbd
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 这是一个特殊的FrameLayout,添加子控件时,可以指定重置大小
    /// </summary>
    public class SpecialFrameLayout : FrameLayout
    {
        /// <summary>
        /// 下面的子控件的底部距离底部边界的距离
        /// </summary>
        public int bottomSpace = 0;
 
        /// <summary>
        /// 这是一个特殊的FrameLayout,添加子控件时,可以指定重置大小
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="i_bottomSpace">
        /// <para>最下面的子控件的底部距离底部边界的距离,当子控件的底部超过边界时,控件会扩大</para>
        /// <para>这是一个理论值,控件内部会计算它的真实值</para>
        /// </param>
        /// <param name="real">是否计算真实值</param>
        public SpecialFrameLayout(int i_Width, int i_Height, int i_bottomSpace, bool real = true)
        {
            this.bottomSpace = Application.GetRealHeight(i_bottomSpace);
 
            if (real == true)
            {
                i_Width = Application.GetRealWidth(i_Width);
                i_Height = Application.GetRealHeight(i_Height);
            }
            this.Height = i_Height;
            this.Width = i_Width;
        }
 
        /// <summary>
        /// 这是一个特殊的FrameLayout,添加子控件时,可以指定重置大小
        /// </summary>
        /// <param name="i_Height">高度</param>
        /// <param name="i_bottomSpace">
        /// <para>最下面的子控件的底部距离底部边界的距离,当子控件的底部超过边界时,控件会扩大</para>
        /// <para>这是一个理论值,控件内部会计算它的真实值</para>
        /// </param>
        /// <param name="real">是否计算真实值</param>
        public SpecialFrameLayout(int i_Height, int i_bottomSpace, bool real = true)
        {
            this.bottomSpace = Application.GetRealHeight(i_bottomSpace);
 
            if (real == true)
            {
                i_Height = Application.GetRealHeight(i_Height);
            }
            this.Height = i_Height;
            this.Width = Application.CurrentWidth;
        }
 
        /// <summary>
        /// 添加子控件
        /// </summary>
        /// <param name="view"></param>
        /// <param name="heightAutoMode">高度变更模式</param>
        public void AddChidren(View view, HeightAutoMode heightAutoMode = HeightAutoMode.None)
        {
            base.AddChidren(view);
 
            //增加当前高度
            if (heightAutoMode == HeightAutoMode.IncreaseOnly)
            {
                //根据增加模式,调整当前控件高度
                this.IncreaseHeightByMode(view);
                //重置控件的位置
                this.ResetGravity(false);
            }
            //自动调整当前高度
            else if (heightAutoMode == HeightAutoMode.AutoOnly)
            {
                //根据自动模式,调整当前控件高度
                this.AutoHeightByMode(view);
                //重置控件的位置
                this.ResetGravity(false);
            }
            //增加当前高度(含全部父容器)
            else if (heightAutoMode == HeightAutoMode.IncreaseAll)
            {
                //根据增加模式,调整当前控件高度
                this.IncreaseHeightByMode(view);
                //根据自动模式,调整全部父容器的高度
                this.IncreaseParentHeightByMode();
                //重置控件的位置
                this.ResetGravity(true);
            }
            //自动调整当前高度(含全部父容器)
            else if (heightAutoMode == HeightAutoMode.AutoAll)
            {
                //根据自动模式,调整当前控件高度
                this.AutoHeightByMode(view);
                //根据自动模式,调整全部父容器的高度
                this.AutoParentHeightByMode();
                //重置控件的位置
                this.ResetGravity(true);
            }
        }
 
        /// <summary>
        /// 根据增加模式,增加当前控件高度
        /// </summary>
        /// <param name="view">进行判定的子控件对象</param>
        public void IncreaseHeightByMode(View view)
        {
            if (view == null)
            {
                //获取容器下最底部的控件
                view = this.GetLastView(this);
            }
            if (view == null)
            {
                return;
            }
 
            //计算最小高度
            int minHeight = view.Bottom + this.bottomSpace;
            if (minHeight <= this.Height)
            {
                //不需要调整高度
                return;
            }
            //调整高度
            this.Height = minHeight;
            if (this.Gravity != Gravity.Frame)
            {
                this.Gravity = this.Gravity;
            }
 
            //如果父容器高度变更,则重置全部子控件的位置
            this.ResetChildrenGravity();
        }
 
        /// <summary>
        /// 根据增加模式,增加当前控件的父容器的高度
        /// </summary>
        public void IncreaseParentHeightByMode()
        {
            View parentView = this.Parent;
            while (parentView != null)
            {
                if ((parentView is SpecialFrameLayout) == false)
                {
                    parentView = parentView.Parent;
                    continue;
                }
                SpecialFrameLayout layout = (SpecialFrameLayout)parentView;
                //根据增加模式,调整当前控件高度
                layout.IncreaseHeightByMode(null);
                //下一个父容器
                parentView = parentView.Parent;
            }
        }
 
        /// <summary>
        /// 根据自动模式,调整当前控件高度
        /// </summary>
        /// <param name="view">进行判定的子控件对象</param>
        public void AutoHeightByMode(View view)
        {
            if (view == null)
            {
                //获取容器下最底部的控件
                view = this.GetLastView(this);
            }
            if (view == null)
            {
                return;
            }
 
            //计算自动高度
            int autoHeight = view.Bottom + this.bottomSpace;
            if (autoHeight == this.Height)
            {
                //不需要调整高度
                return;
            }
            //调整高度
            this.Height = autoHeight;
            //如果父容器高度变更,则重置全部子控件的位置
            this.ResetChildrenGravity();
        }
 
        /// <summary>
        /// 根据自动模式,调整全部父容器的高度
        /// </summary>
        public void AutoParentHeightByMode()
        {
            View parentView = this.Parent;
            while (parentView != null)
            {
                if ((parentView is SpecialFrameLayout) == false)
                {
                    parentView = parentView.Parent;
                    continue;
                }
                SpecialFrameLayout layout = (SpecialFrameLayout)parentView;
                //根据自动模式,调整当前控件高度
                layout.AutoHeightByMode(null);
                //下一个父容器
                parentView = parentView.Parent;
            }
        }
 
        /// <summary>
        /// 获取容器下最底部的控件
        /// </summary>
        /// <param name="specialFrameLayout"></param>
        /// <returns></returns>
        private View GetLastView(SpecialFrameLayout specialFrameLayout)
        {
            if (specialFrameLayout.ChildrenCount == 0)
            {
                return null;
            }
            View lastView = specialFrameLayout.GetChildren(0);
            for (int i = 1; ; i++)
            {
                View view = specialFrameLayout.GetChildren(i);
                if (view == null)
                {
                    break;
                }
                if (lastView.Bottom < view.Bottom)
                {
                    lastView = view;
                }
            }
            return lastView;
        }
 
        /// <summary>
        /// 重置全部子控件的位置
        /// </summary>
        private void ResetChildrenGravity()
        {
            for (int i = 0; ; i++)
            {
                View view = this.GetChildren(i);
                if (view == null)
                {
                    break;
                }
                //重置子控件的位置
                if (view.Gravity != Gravity.Frame)
                {
                    view.Gravity = view.Gravity;
                }
            }
        }
 
        /// <summary>
        /// 重置控件的位置
        /// </summary>
        /// <param name="resetParent">是否重置全部父容器</param>
        private void ResetGravity(bool resetParent)
        {
            //重置控件的位置
            if (this.Gravity != Gravity.Frame)
            {
                this.Gravity = this.Gravity;
            }
 
            if (resetParent == false)
            {
                return;
            }
 
            View parentView = this.Parent;
            while (parentView != null)
            {
                if ((parentView is SpecialFrameLayout) == false)
                {
                    parentView = parentView.Parent;
                    continue;
                }
                //重置控件的位置
                if (parentView.Gravity != Gravity.Frame)
                {
                    parentView.Gravity = parentView.Gravity;
                }
                //下一个父容器
                parentView = parentView.Parent;
            }
        }
    }
}