using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.UserCenter
{
///
/// 这是一个特殊的FrameLayout,添加子控件时,可以指定重置大小
///
public class SpecialFrameLayout : FrameLayout
{
///
/// 下面的子控件的底部距离底部边界的距离
///
public int bottomSpace = 0;
///
/// 这是一个特殊的FrameLayout,添加子控件时,可以指定重置大小
///
/// 宽度
/// 高度
///
/// 最下面的子控件的底部距离底部边界的距离,当子控件的底部超过边界时,控件会扩大
/// 这是一个理论值,控件内部会计算它的真实值
///
/// 是否计算真实值
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;
}
///
/// 这是一个特殊的FrameLayout,添加子控件时,可以指定重置大小
///
/// 高度
///
/// 最下面的子控件的底部距离底部边界的距离,当子控件的底部超过边界时,控件会扩大
/// 这是一个理论值,控件内部会计算它的真实值
///
/// 是否计算真实值
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;
}
///
/// 添加子控件
///
///
/// 高度变更模式
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);
}
}
///
/// 根据增加模式,增加当前控件高度
///
/// 进行判定的子控件对象
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();
}
///
/// 根据增加模式,增加当前控件的父容器的高度
///
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;
}
}
///
/// 根据自动模式,调整当前控件高度
///
/// 进行判定的子控件对象
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();
}
///
/// 根据自动模式,调整全部父容器的高度
///
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;
}
}
///
/// 获取容器下最底部的控件
///
///
///
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;
}
///
/// 重置全部子控件的位置
///
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;
}
}
}
///
/// 重置控件的位置
///
/// 是否重置全部父容器
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;
}
}
}
}