2016-12-06 68 views
0

首先,我在Xamarin for Android应用程序中编写代码。在布局中获取TextView的高度

我试过的是这个。

frame.AddView(background, LayoutParams.MatchParent, textViewA.Height); 
frame.AddView(textViewA, LayoutParams.MatchParent, LayoutParams.WrapContent); 

1)背景是具有背景的视图。的FrameLayout的帧(例如)被设置

2)后,我添加此布局为tablerow

的问题是它。由于textViewA.Height的值为0,所以我看不到背景。 enter image description here

如果我改变textViewA.Height到LayoutParams.WrapContentLayoutParams.MatchParent,那么背景覆盖孔页。

enter image description here

,如果我改变textViewA.Height 15,背景涵盖TextView中的一部分。

enter image description here

所以,(我认为)我必须知道textViewA的高度值的大小,使显示的背景。 如何做到这一点?帮我。

回答

1

我必须知道textViewA的高度值的大小才能显示背景。怎么做?

我们无法直接获取TextView的高度,因为TextView甚至可能没有被测量到。

如果你真的想要得到textViewA的身高请尝试以下代码

ViewTreeObserver vto = textViewA.ViewTreeObserver; 
vto.GlobalLayout += (sender, args) => 
{ 
    myTextViewHeight = textViewA.Height; 
}; 

但你衡量textViewA在FrameLayout里。当你得到“myTextViewHeight”时,你的布局已经被测量。你仍然会得到0高度,你在的FrameLayout

添加它作为一种解决办法之前

  1. 你可以设置“textViewA.Height”你的FrameLayout添加之前

  2. 您可以添加您作为默认:

    frame.AddView(textViewA, LayoutParams.MatchParent, LayoutParams.WrapContent); 
    frame.AddView(background); 
    

顺便说一句,如果你只需要设置你的表格的背景,在你的表格中添加textview或者设置表格背景可能是一个更好的选择。

+0

谢谢,非常感谢! – wallah

相关问题