[英]how to fix resolution in application?
I wanted to know how the application can be displayed one size on all devices?我想知道应用程序如何在所有设备上以一种尺寸显示?
Because when I test my app on small devices, all the elements are either too big or do not fit on the screen.因为当我在小型设备上测试我的应用程序时,所有元素要么太大要么不适合屏幕。
What should be done to solve this problem?应该怎样做才能解决这个问题?
I used StackLayout
to arrange the elements.我使用
StackLayout
来排列元素。
This is one of my codes:这是我的代码之一:
<StackLayout Orientation="Vertical" Margin="10" Padding="10" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Label Text="WellCome to App" TextColor="Aquamarine" FontSize="40" FontAttributes="Bold,Italic"
HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Label Text="Please select the topic you want:" FontSize="21" FontAttributes="Bold,Italic"
HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" TextColor="DarkRed"/>
<Grid ColumnSpacing="0" RowSpacing="0" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="120"/>
<RowDefinition Height="120"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="120"/>
</Grid.ColumnDefinitions>
<Button Text="Space" TextColor="White" FontSize="Large"
BackgroundColor="Black"
HeightRequest="140"
WidthRequest="140"
CornerRadius="60"
HorizontalOptions="Center"
BorderWidth="1"
BorderColor="Silver"
Clicked="spaceButton_OnClicked">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
<Button Text="Sports" FontSize="Large" Grid.Column="2"
TextColor="White" BackgroundColor="Black"
HeightRequest="140"
WidthRequest="140"
CornerRadius="60"
HorizontalOptions="Center"
BorderWidth="1"
BorderColor="Silver"
Clicked="sportButton_OnClicked">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStatess">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
<Button Text="Game" FontSize="Large" Grid.Row="1" Grid.Column="1"
TextColor="White" BackgroundColor="Black"
HeightRequest="140"
WidthRequest="140"
CornerRadius="60"
HorizontalOptions="Center"
BorderWidth="1"
BorderColor="Silver"
Clicked="gameButton_OnClicked">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStatesss">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
<Button Text="Technology" FontSize="12" Grid.Row="2" Grid.Column="0"
TextColor="White" BackgroundColor="Black"
HeightRequest="140"
WidthRequest="140"
CornerRadius="60"
HorizontalOptions="Center"
BorderWidth="1"
BorderColor="Silver"
Clicked="techButton_OnClicked">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStats">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
<Button Text="History" FontSize="20" Grid.Row="2" Grid.Column="2"
TextColor="White" BackgroundColor="Black"
HeightRequest="140"
WidthRequest="140"
CornerRadius="60"
HorizontalOptions="Center"
BorderWidth="1"
BorderColor="Silver"
Clicked="oldButton_OnClicked">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonSttes">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
</Grid
</StackLayout>
My code is displayed in this form:我的代码以这种形式显示:
From shared code, the proportions of the elements are the same.Then you could set Star ( *
) for Height
and Width
, and set HorizontalOptions
and VerticalOptions
of Button
be FillAndExpand
to make the elements to fit all the sizes of device screen.从共享代码来看,元素的比例是相同的。那么您可以为
Height
和Width
设置星号( *
),并将Button
的 Horizo HorizontalOptions
和VerticalOptions
设置为FillAndExpand
以使元素适合所有设备屏幕尺寸。
However, only setting this can not make the Height
of Grid Item shows the same with Width:( Pixel 2 Pie 9.0 420dpi )但是,仅设置此项不能使Grid Item 的
Height
与宽度显示相同:( Pixel 2 Pie 9.0 420dpi )
To solve this you could calcuate the Width
and Height
on runtime.要解决这个问题,您可以在运行时计算
Width
和Height
。 From this discussion , you will know if the units of Width
and Height
set a specified value, it will show different effects in different device.从这个讨论中,你会知道如果
Width
和Height
的单位设置了一个指定的值,在不同的设备上会表现出不同的效果。
Luckly, we can get Screen Density easily by using Xamarin.Essentials .幸运的是,我们可以使用Xamarin.Essentials轻松获得屏幕密度。 If you need a 120 units value and based on Pixel 2 Pie 9.0 420dpi (1080*1920) android device, you can set
Height
or Width
as follows:如果您需要 120 个单位的值并且基于Pixel 2 Pie 9.0 420dpi (1080*1920) android 设备,您可以设置
Height
或Width
如下:
// this device screen density is 2.625, therefore divide by 2.625, if you test in other device, you could divide by its screen density.
Row.Height/Width = 120 * (mainDisplayInfo.Density / 2.625)
Then it will show correctly:然后它会正确显示:
The full XML Code as follows:完整的XML代码如下:
<StackLayout Orientation="Vertical"
Margin="10"
Padding="10"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<Label Text="WellCome to App"
TextColor="Aquamarine"
FontSize="40"
FontAttributes="Bold,Italic"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand" />
<Label Text="Please select the topic you want:"
FontSize="21"
FontAttributes="Bold,Italic"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
TextColor="DarkRed" />
<Grid ColumnSpacing="0"
RowSpacing="0"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand">
<Grid.RowDefinitions>
<RowDefinition x:Name="RowOne" Height="*" />
<RowDefinition x:Name="RowTwo" Height="*" />
<RowDefinition x:Name="RowThree" Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="ColumnOne" Width="*" />
<ColumnDefinition x:Name="ColumnTwo" Width="*" />
<ColumnDefinition x:Name="ColumnThree" Width="*" />
</Grid.ColumnDefinitions>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Button Text="Space"
TextColor="White"
FontSize="Large"
BackgroundColor="Black"
HorizontalOptions="FillAndExpand"
CornerRadius="60"
VerticalOptions="FillAndExpand"
BorderWidth="1"
BorderColor="Silver">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
</StackLayout>
<Button Text="Sports"
FontSize="Large"
Grid.Column="2"
TextColor="White"
BackgroundColor="Black"
VerticalOptions="FillAndExpand"
CornerRadius="60"
HorizontalOptions="FillAndExpand"
BorderWidth="1"
BorderColor="Silver">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStatess">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
<Button Text="Game"
FontSize="Large"
Grid.Row="1"
Grid.Column="1"
TextColor="White"
BackgroundColor="Black"
VerticalOptions="FillAndExpand"
CornerRadius="60"
HorizontalOptions="FillAndExpand"
BorderWidth="1"
BorderColor="Silver">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStatesss">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
<Button Text="Technology"
FontSize="12"
Grid.Row="2"
Grid.Column="0"
TextColor="White"
BackgroundColor="Black"
VerticalOptions="FillAndExpand"
CornerRadius="60"
HorizontalOptions="FillAndExpand"
BorderWidth="1"
BorderColor="Silver">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStats">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
<Button Text="History"
FontSize="20"
Grid.Row="2"
Grid.Column="2"
TextColor="White"
BackgroundColor="Black"
VerticalOptions="FillAndExpand"
CornerRadius="60"
HorizontalOptions="FillAndExpand"
BorderWidth="1"
BorderColor="Silver">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonSttes">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale"
Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale"
Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
</Grid>
</StackLayout>
And the ContentPage.cs :和ContentPage.cs :
public partial class PageTestGrid : ContentPage
{
public PageTestGrid()
{
InitializeComponent();
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var density = mainDisplayInfo.Density;
//if you want to set 120 as the Height of Grid
RowOne.Height = 120 * (mainDisplayInfo.Density / 2.625);
RowTwo.Height = 120 * (mainDisplayInfo.Density / 2.625);
RowThree.Height = 120 * (mainDisplayInfo.Density / 2.625);
ColumnOne.Width = 120 * (mainDisplayInfo.Density / 2.625);
ColumnTwo.Width = 120 * (mainDisplayInfo.Density / 2.625);
ColumnThree.Width = 120 * (mainDisplayInfo.Density / 2.625);
}
}
The effect on Phone H-dpi 4in Pie 9.0 H-dpi(480*800) : Phone H-dpi 4in Pie 9.0 H-dpi(480*800)效果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.