设置选择器的背景图像

时间:2017-08-13 03:34:43

标签: xamarin xamarin.ios xamarin.android xamarin.forms

我想设置这样的背景图片。

这是我的选择器的背景:

我尝试使用此代码:

<Grid >
<Image Source="input_mobile_code_brown.png" x:Name="img"></Image>
<Picker></Picker>
</Grid>

但它首先显示了选择器,图像像堆栈布局方向水平。

1 个答案:

答案 0 :(得分:3)

您可以使用 RelativeLayout 来找到您的控件。我为您编写了一个示例,您可以编辑RelativeLayout以适应您的场景。

    <RelativeLayout Margin="0,20,0,0">
        <Image Source="pickerbackgroundimage.png" Aspect="AspectFit"  x:Name="img"
                   RelativeLayout.XConstraint =
                        "{ConstraintExpression Type=RelativeToParent,
                             Property=Width,
                             Factor=0,
                             Constant=0}"
                    RelativeLayout.YConstraint =
                        "{ConstraintExpression Type=RelativeToParent,
                             Property=Height,
                             Factor=0,
                             Constant=0}"
               />


        <Picker BackgroundColor="Transparent" x:Name="picker" 
                    RelativeLayout.XConstraint =
                        "{ConstraintExpression Type=RelativeToParent,
                             Property=Width,
                             Factor=0,
                             Constant=0}"
                    RelativeLayout.YConstraint =
                        "{ConstraintExpression Type=RelativeToParent,
                             Property=Height,
                             Factor=0,
                             Constant=0}"
                    RelativeLayout.WidthConstraint =
                        "{ConstraintExpression Type=RelativeToView,
                             ElementName=img,
                             Property=Width,
                             Factor=1,
                             Constant=0}"
                   RelativeLayout.HeightConstraint =
                        "{ConstraintExpression Type=RelativeToView,
                             ElementName=img,
                             Property=Height,
                             Factor=1,
                             Constant=0}"
                />

     </RelativeLayout>

它在iOS和Android上的工作原理如下: enter image description here