Xamarin.Forms自定义特定视图的填充

时间:2016-06-24 16:00:04

标签: xamarin.forms

所以我在Xamarin.Forms开发新手,我正在开发一个项目,我在一个页面上的StackLayout中有多个视图。我想知道是否有一种方法可以为每个不同的视图指定不同的填充。

例如,我想要填充整个屏幕宽度的图像,但是下面有一个条目,我不想填满整个屏幕。但是,如果我使用StackLayout.Padding属性,它会为所有视图设置相同的填充。

有没有解决方案?

修改 我之前忘了提到我已经尝试过使用保证金属性但是仍然收到错误' Entry'不包含' margin'

的定义

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace HuntFishNY
{
class LandingPage : ContentPage
{
    Image logo, silhouettes;
    Entry username, password;
    Button signIn, register;
    Label title, trouble;
    StackLayout heading, inputSection, footer;

    public LandingPage()
    {
        this.BackgroundColor = Color.FromHex("#2B5237");


        logo = new Image();
        logo.Source = "dec_logo.png";


        silhouettes = new Image();
        silhouettes.Source = "sportinglicense_background.jpg";


        username = new Entry();
        username.HorizontalTextAlignment = TextAlignment.Start;
        username.BackgroundColor = Color.White;
        username.Placeholder = "Username";
        username.PlaceholderColor = Color.Gray;
        username.TextColor = Color.Black;
        username.VerticalOptions = LayoutOptions.FillAndExpand;



        password = new Entry();
        password.HorizontalTextAlignment = TextAlignment.Start;
        password.IsPassword = true;
        password.BackgroundColor = Color.White;
        password.Placeholder = "Password";
        password.PlaceholderColor = Color.Gray;
        password.TextColor = Color.Black;
        password.VerticalOptions = LayoutOptions.FillAndExpand;

        signIn = new Button();
        signIn.Text = "Sign In";
        signIn.BackgroundColor = Color.FromHex("#2b5237");

        register = new Button();
        register.Text = "Register New Account";

        title = new Label();

        title.TextColor = Color.FromHex("#E2AF28");
        title.HorizontalTextAlignment = TextAlignment.Center;

        trouble = new Label();
        trouble.Text = "Having trouble signing in?";

        heading = new StackLayout();
        heading.VerticalOptions = LayoutOptions.Start;

        heading.Children.Add(logo);
        heading.Children.Add(title);
        heading.Children.Add(silhouettes);
        heading.Children.Add(username);
        heading.Children.Add(password);
        heading.Padding = new Thickness(10, 10, 20, 20);


        this.Content = heading;








        }


    }
}

1 个答案:

答案 0 :(得分:1)

您似乎应该使用Gridhttps://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/grid/)而不是StackLayout

您可以使用RowDefinitions内的Grid来获得不同的行高。

您还可以在另一个StackLayout内添加StackLayout

也就是说,如果您的Entry没有Margin,可能是因为您正在使用过时的Xamarin.Forms 版本(保证金是在2.1或2.2中引入。当前是2.3)

相关问题