2016-12-06 72 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

相關問題