如何使用Xamarin Forms放置此标签和图像?

时间:2015-05-31 09:31:52

标签: xamarin.forms

我正在尝试学习Xamarin表单并希望尝试执行以下操作:

enter image description here

我认为我需要使用RelativeLayout,但我很难理解 如何使用提供的工具。我正在阅读的所有示例都说使用LayoutFlagsLayoutBounds之类的内容(例如this nice demoalso this one)。

但是没有人在解释我们应该如何使用它们。

有人可以解释(奖励积分,如果他们提供帮助线的图像等)我怎么能做我正在尝试的那张图片?

Image: it's like it's anchored to the bottom right corner

Label: it's like it's centered .. and pushed down from the top about 20% odd..

2 个答案:

答案 0 :(得分:0)

这里有一些不错的例子:SyntaxIsMyUI。你可以在那里相对于你的父和父。宽度来做。例如。

否则,您可以根据此轻松获得屏幕大小:Xamarin.Forms Kickstarter

祝你好运!

更新

public class MyPage : ContentPage
{
    public MyPage ()
    {
        Label label = new Label { 
            BackgroundColor = Color.Blue,
            Text = "Label",
            XAlign = TextAlignment.Center,
        };
        BoxView image = new BoxView { 
            BackgroundColor = Color.Aqua,
        };
        BoxView frame = new BoxView { 
            BackgroundColor = Color.Blue,
            HeightRequest = 2000, //this will be ignored in relLayout
        };

        RelativeLayout relLayout = new RelativeLayout ();                   //create your relative layout which will be your parent
        relLayout.BackgroundColor = Color.Red;                              //this will only be full screen if this is your only View

        relLayout.Children.Add (label,                                      //add the children of it
            Constraint.RelativeToParent ((Parent) => Parent.Width / 4),     //left side of label a quarter in on screen
            Constraint.RelativeToParent ((Parent) => Parent.Height / 5),    //pushed down a fifth (20%) on the screen
            Constraint.RelativeToParent ((Parent) => Parent.Width / 2));    //Width is half screen size

        relLayout.Children.Add (frame,                                      //what will be a frame around picture
            Constraint.RelativeToParent ((Parent) => Parent.Width / 2),     //left side begin in the middle of screen along x-axis
            Constraint.RelativeToParent ((Parent) => Parent.Height - 60),   //top side 60 dp from bottom of the view (parent, this case entire screen)
            Constraint.RelativeToParent ((Parent) => Parent.Width / 2-10),  //Width is half screen - 30 dp to fit inside the frame
            Constraint.Constant (50));                                      //Height set to exactly 50 dp 

        relLayout.Children.Add (image,                                      //your image
            Constraint.RelativeToView (frame, 
                new Func<RelativeLayout, View, double> ((prelLayout, pframe) => frame.X + 5)), //inside the frame by 10 db
            Constraint.RelativeToView (frame,
                new Func<RelativeLayout, View, double> ((prelLayout, pframe) => frame.Y + 5)),  //10 dp inside frame
            Constraint.RelativeToView (frame,
                new Func<RelativeLayout, View, double> ((prelLayout, pframe) => frame.Width - 10)),//Width is same as frame - 10 dp (5 on each side)
            Constraint.RelativeToView (frame,
                new Func<RelativeLayout, View, double> ((prelLayout, pframe) => frame.Height - 10)));//Height set to same as frame height-10 to add the 5 dp inside the frame

        this.Content = relLayout;
    }
}

答案 1 :(得分:0)

      img = new Image { Source = "a.png" ,Aspect=Aspect.AspectFill };

        txt = new Label { Text = "0",FontSize=30, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center, TextColor = Color.Black };



grid = new Grid { 
           HorizontalOptions = LayoutOptions.CenterAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand
             };
        grid.RowDefinitions.Add(new RowDefinition());
        grid.RowDefinitions.Add(new RowDefinition());
        grid.Children.Add(img);

        grid.Children.Add(txt);
    Content = new StackLayout
        {
            BackgroundColor=Color.White,
            Children = {
             grid,