如何在Xamarin.Forms中添加背景图像到Entry?

时间:2015-07-10 19:29:57

标签: c# xaml xamarin xamarin.forms

我的设计师创建了一个背景图片来设置输入样式,如下所示:

enter image description here

我怎样才能在Xamarin.Forms中实现这一目标? 我猜测的简单部分是移除边框,但我还需要将其放置在该图像的顶部(我假设使用RelativeLayout?)并且最难(在我的观点中)设置其正确尺寸为"填充"剩下的图像。 我使用的是XAML,但是如果你在C#中知道我可以推断出来,没问题。 有什么想法吗?

编辑: 这就是我迄今为止所做的事情: enter image description here

代码:

<RelativeLayout>
  <Image Source="input_selected.png"></Image>
  <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
    <Image Source="ico_password.png" Scale="0.7"></Image>
    <Entry x:Name="PasswordT" Placeholder="Senha" IsPassword="True" HorizontalOptions="CenterAndExpand" Text=""></Entry>
  </StackLayout>
</RelativeLayout>

EDIT2:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="LoginPage"
             Style="{StaticResource LoginBackground}">
  <ScrollView>
    <RelativeLayout x:Name="RelativeLayoutLogin">
      <StackLayout x:Name="BackgroundLayout" Spacing="15" HorizontalOptions="Center" VerticalOptions="Start">
        <StackLayout.Padding>
          <OnPlatform x:TypeArguments="Thickness" iOS="15, 38, 15, 15" Android="15, 18, 15, 15" WinPhone="15, 18, 15, 15" />
        </StackLayout.Padding>
        <StackLayout.Children>
          <Image Source="logo.png" Aspect="AspectFit" HeightRequest="75"></Image>
          <Image x:Name="BackgroundBox" Source="box_completo.png" Aspect="AspectFill"></Image>
        </StackLayout.Children>
      </StackLayout>

      <StackLayout RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=BackgroundLayout, Property=Y, Constant=111}"
                   RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToView, ElementName=BackgroundLayout, Property=Width, Factor=1}">
        <!--<StackLayout.Padding>
          <OnPlatform x:TypeArguments="Thickness" iOS="15, 38, 15, 15" Android="15, 18, 15, 15" WinPhone="15, 18, 15, 15" />
        </StackLayout.Padding>-->
        <RelativeLayout>
          <Image Source="input.png" x:Name="BgUsername"></Image>
          <Image Source="input_selected.png" x:Name="SelectedBgUsername" IsVisible="False"></Image>
          <StackLayout RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=8}"
                    RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=8}"
                    RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Constant=-16}"
                    RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Constant=-16}"
                    Orientation="Horizontal">
            <Image Source="ico_user.png" Scale="0.6"></Image>
            <Entry x:Name="UsernameOrEmail" Placeholder="Nome de usuário" IsEnabled="True" HorizontalOptions="FillAndExpand" Text="" TextColor="Black"></Entry>
          </StackLayout>
        </RelativeLayout>
        <RelativeLayout>
          <Image Source="input.png" x:Name="BgPassword"></Image>
          <Image Source="input_selected.png" x:Name="SelectedBgPassword" IsVisible="False"></Image>
          <StackLayout RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=8}"
                      RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=8}"
                      RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Constant=-16}"
                      RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Constant=-16}"
                      Orientation="Horizontal">
            <Image Source="ico_password.png" Scale="0.6"></Image>
            <Entry x:Name="Password" Placeholder="Senha" IsPassword="True" HorizontalOptions="FillAndExpand" Text="" TextColor="Black"></Entry>
          </StackLayout>
        </RelativeLayout>
        <!--<RelativeLayout Scale="0.85">
          <Image Source="input.png" x:Name="BgUsername"></Image>
          <Image Source="input_selected.png" x:Name="SelectedBgUsername" IsVisible="False"></Image>
          <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" Padding="15, 5, 0, 0">
            <Image Source="ico_user.png" Scale="0.6"></Image>
            <Entry x:Name="UsernameOrEmail" Placeholder="Nome de usuário" IsEnabled="True" WidthRequest="300" HorizontalOptions="CenterAndExpand" Text="" TextColor="Black"></Entry>
          </StackLayout>
        </RelativeLayout>
        <RelativeLayout Scale="0.85">
          <Image Source="input.png" x:Name="BgPassword"></Image>
          <Image Source="input_selected.png" x:Name="SelectedBgPassword" IsVisible="False"></Image>
          <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" Padding="10, 5, 0, 0">
            <Image Source="ico_password.png" Scale="0.6"></Image>
            <Entry x:Name="Password" Placeholder="Senha" IsPassword="True" HorizontalOptions="CenterAndExpand" WidthRequest="300" Text="" TextColor="Black"></Entry>
          </StackLayout>
        </RelativeLayout>-->
        <Image x:Name="LoginButtonFm" Style="{StaticResource FmLoginButton}" Scale="0.85"></Image>
        <Label Text="OU" TextColor="Black" HorizontalOptions="CenterAndExpand"></Label>
        <Image x:Name="LoginButtonFacebook" Style="{StaticResource FacebookLoginButton}" Scale="0.85"></Image>
        <Image x:Name="LoginButtonGoogle" Style="{StaticResource GoogleLoginButton}" Scale="0.85"></Image>
        <Image x:Name="LoginButtonTwitter" Style="{StaticResource TwitterLoginButton}" Scale="0.85"></Image>
      </StackLayout>
    </RelativeLayout>
  </ScrollView>
</ContentPage>

谢谢!

1 个答案:

答案 0 :(得分:8)

你肯定是在正确的轨道上。只需要在View中的RelativeLayout之一添加一些约束。

<RelativeLayout>
  <Image Source="input_selected.png"></Image>
    <StackLayout
      RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=8}"
      RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=8}"
      RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Constant=-16}"
      RelativeLayout.HeightConstraint="{ConstraintExpression Type=Constant, Constant=48}"
      Orientation="Horizontal">
      <Image Source="ico_password.png" Scale="0.7"></Image>
      <Entry x:Name="PasswordT" Placeholder="Senha" IsPassword="True" HorizontalOptions="FillAndExpand" Text="offspring"></Entry>
    </StackLayout>
</RelativeLayout>

这将定位StackLayout以在每一侧填充8dpx填充的整个RelativeLayout。然后,因为Entry设置为FillAndExpand,它将占用{icer_password未占用的StackLayout中的所有空格。