2017-02-13 87 views
2

我想通過Codebihnd中的AbsoluteLayout在BoxView組件上添加文本。 類似這樣的: enter image description here如何將標籤文本添加到圓括號框中

我該怎麼辦?

這裏是我的代碼,在那裏我把absoluteLayout到stackLayout底:

public TestPageBoxView() 
    { 
     StackLayout sl = new StackLayout(); 
     AbsoluteLayout al = new AbsoluteLayout(); 

     BoxView bv = new BoxView { BackgroundColor = Color.Green }; 
     Label l = new Label { Text = "Some text with \n breaks" }; 

     AbsoluteLayout.SetLayoutBounds(l, new Rectangle(0, 0,1,1)); 
     AbsoluteLayout.SetLayoutFlags(l, AbsoluteLayoutFlags.All); 

     sl.Children.Add(bv); 
     sl.Children.Add(al); 
     Content = sl; 
    } 

所以,我擔心的是,這個L成分粘到stacklayout而不是BoxView中。

也許我應該嘗試RelativeLayout的?

你知道混帳一些組件?像XLabs?

謝謝。

+0

其實我的應用程序,我用來代替按鈕 –

+0

@MikeDarwish的ImageButton有任何限制,沒有ü檢查例如多少大的文本可以持有? Tnx – Stefan0309

+0

標準圖像按鈕有限制。它大多不適合。在Mozilla中有一些開放的bug,如果你想在圖像中使用文本,它只在圖像右側放置文本。我記得上次不可能改變。如果您需要圖像按鈕,則應該使用輕拍手勢識別器的圖像。 – batmaci

回答

0

我沒有測試C#代碼,但是我在XAML確切的代碼和它的作品對我來說。您需要將boxview和label作爲absolutelayout的子項,並且您必須設置如下所示的一些absolutelayout選項,以使它們彼此重疊。當然,首先添加boxview標籤應該覆蓋它。 標籤的背景顏色必須爲透明

最後一件事,我不知道,但我認爲BoxView中的行得有舍入選項爲默認值。你需要一些自定義渲染器。在下面的例子中,我使用了將styleclass作爲圓圈暴露的xamarin light主題。否則搜索customrenderer或有一些nugetpackages可用。 xlab有一個我的猜測。我希望這可以幫助你。

C#

AbsoluteLayout al = new AbsoluteLayout() 
      { 
       WidthRequest = 30, 
       BackgroundColor = Color.White, 
       HorizontalOptions = LayoutOptions.Center, 
       VerticalOptions = LayoutOptions.Center 
      }; 

      BoxView bv = new BoxView 
      { 
       StyleClass = "Circle" , 
       BackgroundColor = Color.Green, 
       HorizontalOptions = LayoutOptions.Center, 
       VerticalOptions = LayoutOptions.Center 
      }; 

      AbsoluteLayout.SetLayoutBounds(bv, new Rectangle(.5, .5, 1, 1)); 
      AbsoluteLayout.SetLayoutFlags(bv, AbsoluteLayoutFlags.All); 


      Label l = new Label { Text = "1", BackgroundColor = Color.Transparent }; 
      AbsoluteLayout.SetLayoutBounds(l, new Rectangle(.5, .5, 1, 1)); 
      AbsoluteLayout.SetLayoutFlags(l, AbsoluteLayoutFlags.All); 
      al.Children.Add(bv); 
      al.Children.Add(l); 
相關問題