【问题标题】:Xamarin.Forms Customize Padding for a Specific ViewXamarin.Forms 为特定视图自定义填充
【发布时间】:2016-06-25 18:31:27
【问题描述】:

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

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

有什么解决办法吗?

编辑: 我之前忘了提到我已经尝试过使用边距属性,但不断收到错误“条目”不包含“边距”的定义

这是我的代码:

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) 为每个视图使用边距,而不是为 stacklayout 填充 2) 将您的视图(控件)放在其他视图中,例如(ContentView 或其他 StackLayout)并给它们填充。3)您可以通过嵌套视图创建非常复杂的布局,但要小心不利于性能
  • 我尝试使用边距属性,但一直收到错误消息,提示“条目”不包含“边距”的定义

标签: xamarin.forms


【解决方案1】:

看来您应该使用Grid(https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/grid/) 而不是StackLayout

您可以在Grid 中使用RowDefinitions 实现不同的行高。

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

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

【讨论】:

  • 只是为了确认,2.2.0+的边距