ios 如何在 Xamarin.Forms 中设置控件的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25663347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set width of controls in Xamarin.Forms
提问by Nirav Mehta
I've tried to adjust width & height of my textbox [Entry] control in Xamarin.Forms for iPad app but it doesn't set the width using WidthRequest property.
我试图在 Xamarin.Forms for iPad 应用程序中调整我的文本框 [Entry] 控件的宽度和高度,但它没有使用 WidthRequest 属性设置宽度。
Can anyone please help me for the same about how to set the width of the controls.
任何人都可以帮助我了解如何设置控件的宽度。
Here is my code of XAML for same using Grid & StackLayout but none of them worked.
这是我使用 Grid & StackLayout 的相同 XAML 代码,但它们都不起作用。
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" ></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="10"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="loginlogo.png"></Image>
<StackLayout Grid.Row="1" Grid.Column="0">
<Entry Placeholder="Enter Username" WidthRequest="20"></Entry>
<Entry Placeholder="Enter Password" WidthRequest="20"></Entry>
<Button Text="Button 1"></Button>
</StackLayout>
</Grid>
Also tried with Stack Layout
也尝试过堆栈布局
<StackLayout Spacing="10">
<Entry x:Name="txtUserName" Placeholder="Enter Username" WidthRequest="2" HeightRequest="200"></Entry>
<Entry x:Name="txtPassword" Placeholder="Enter Password" WidthRequest="2" HeightRequest="200"></Entry>
<Button Text="Button 1"></Button></StackLayout>
回答by Pete
Note that you have to specify HorizontalOptionswith the WidthRequestto get this to work on a StackLayoutas it will attempt to auto-expand by default.
请注意,您必须指定HorizontalOptions与WidthRequest得到这个工作在StackLayout默认情况下,它会尝试自动扩展。
Example shown below for StackLayout:-
下面显示的StackLayout 示例:-
StackLayout objStackLayout = new StackLayout()
{
Spacing = 10
};
//
Entry objEntry1 = new Entry()
{
Placeholder = "Enter Username",
WidthRequest = 300,
HeightRequest = 200,
HorizontalOptions = LayoutOptions.Start
};
objStackLayout.Children.Add(objEntry1);
//
Entry objEntry2 = new Entry()
{
Placeholder = "Enter Password",
WidthRequest = 200,
HeightRequest = 200,
HorizontalOptions = LayoutOptions.Start
};
objStackLayout.Children.Add(objEntry2);
//
Button objButton1 = new Button()
{
Text = "Button1",
HorizontalOptions = LayoutOptions.Start
};
objStackLayout.Children.Add(objButton1);
回答by Sergey Metlov
Use StackLayout
with combination of spacings, horizontal/vertical options and paddings. Example:
StackLayout
与间距、水平/垂直选项和填充组合使用。例子:
<StackLayout Padding="20" Spacing="10">
<Entry Placeholder="Username"
VerticalOptions="Start"
HorizontalOptions="FillAndExpand" />
<Entry Placeholder="Password"
VerticalOptions="Start"
HorizontalOptions="FillAndExpand" />
<Button Text="Submit" VerticalOptions="EndAndExpand" />
</StackLayout>