C# 在布局中渲染TextView之前获取其高度
首先,我正在用Xamarin为Android应用程序编写代码 我试过的就是这个C# 在布局中渲染TextView之前获取其高度,c#,android,xamarin,C#,Android,Xamarin,首先,我正在用Xamarin为Android应用程序编写代码 我试过的就是这个 frame.AddView(background, LayoutParams.MatchParent, textViewA.Height); frame.AddView(textViewA, LayoutParams.MatchParent, LayoutParams.WrapContent); 1) 背景是具有背景的视图 2) 设置frame(FrameLayout的实例)后,我将此布局添加到TableRow 问
frame.AddView(background, LayoutParams.MatchParent, textViewA.Height);
frame.AddView(textViewA, LayoutParams.MatchParent, LayoutParams.WrapContent);
1) 背景是具有背景的视图
2) 设置frame(FrameLayout的实例)后,我将此布局添加到TableRow
问题在于它。由于textViewA.Height的值为0,因此我看不到背景。
如果我将textViewA.Height更改为LayoutParams.WrapContent或LayoutParams.MatchParent,则背景会覆盖整个页面
如果我把textViewA.Height改为15,背景会覆盖textView的一部分
所以,(我想)我必须知道textViewA的高度值的大小才能显示背景。
怎么做?帮帮我
我必须知道textViewA的高度值的大小才能显示背景。怎么做
我们无法直接获得TextView的高度,因为那时TextView甚至可能还没有被测量过
如果您真的想获得textViewA的高度,请尝试以下代码
ViewTreeObserver vto = textViewA.ViewTreeObserver;
vto.GlobalLayout += (sender, args) =>
{
myTextViewHeight = textViewA.Height;
};
但是您可以在FrameLayout中测量textViewA。当您获得“myTextViewHeight”时,您的布局已被测量。在将0高度添加到FrameLayout中之前,仍将获得该高度
作为解决办法
frame.AddView(textViewA, LayoutParams.MatchParent, LayoutParams.WrapContent);
frame.AddView(background);
ViewTreeObserver vto = textViewA.ViewTreeObserver;
vto.GlobalLayout += (sender, args) =>
{
myTextViewHeight = textViewA.Height;
};
但是您可以在FrameLayout中测量textViewA。当您获得“myTextViewHeight”时,您的布局已被测量。在将0高度添加到FrameLayout中之前,仍将获得该高度
作为解决办法
frame.AddView(textViewA, LayoutParams.MatchParent, LayoutParams.WrapContent);
frame.AddView(background);