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;
|
}
|
}
|
}
|
}
|