Android Xamarin表单应用程序纵向页面从横向返回时宽度更大
我正在Visual Studio 2015中使用Xamarin表单开发我的第一个应用程序 我有一个带有网格的页面,它在StackLayout中,它在ScrollView中,它在AbsoluteLayout中 以下是该页面的构造函数:Android Xamarin表单应用程序纵向页面从横向返回时宽度更大,android,xamarin,xamarin.android,xamarin.forms,Android,Xamarin,Xamarin.android,Xamarin.forms,我正在Visual Studio 2015中使用Xamarin表单开发我的第一个应用程序 我有一个带有网格的页面,它在StackLayout中,它在ScrollView中,它在AbsoluteLayout中 以下是该页面的构造函数: public ConditionsPage(long reportId) { InitializeComponent(); Title = "New Entry"; this
public ConditionsPage(long reportId)
{
InitializeComponent();
Title = "New Entry";
this.ReportId = reportId;
var columns =
DependencyService.Get<IGetReports.IGetReportsService>().GetAllVisibleColumns(ReportId);
Rows = new List<RowEntry>();
foreach (var c in columns)
{
Label label = new Label
{
Text = c.ColumnName,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
TextColor = Color.Black
};
Picker picker = new Picker
{
Title = "Condition",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
TextColor = Color.Black
};
foreach (string condition in conditionOperators.Values)
{
picker.Items.Add(condition);
}
// Create Entry for condition value input
Entry rowEntry = new Entry()
{
WidthRequest = 100,
HeightRequest = 50,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
TextColor = Color.Black,
};
if (c.ColumnType == ReportColumnType.Datetime)
{
rowEntry.Keyboard = Keyboard.Text;
}
else if (c.ColumnType == ReportColumnType.Text)
{
rowEntry.Keyboard = Keyboard.Text;
}
else
{
rowEntry.Keyboard = Keyboard.Numeric;
}
Rows.Add(new RowEntry() {ColumnId = c.Id, Entry = rowEntry, Label = label, Picker = picker });
}
// Build the page.
var absoluteLayout = new AbsoluteLayout();
var stackLayout = new StackLayout();
var scrollView = new ScrollView();
var grid = new Grid()
{
VerticalOptions = LayoutOptions.Fill,
HorizontalOptions = LayoutOptions.Fill,
RowDefinitions =
{
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
new RowDefinition { Height = new GridLength(100, GridUnitType.Absolute) }
},
ColumnDefinitions =
{
new ColumnDefinition { Width = GridLength.Auto },
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(100, GridUnitType.Absolute) }
}
};
int i = 0;
foreach (var row in Rows)
{
grid.Children.Add(row.Label, 0, i);
grid.Children.Add(row.Picker, 1, i);
grid.Children.Add(row.Entry, 2, i);
i++;
}
stackLayout.Children.Add(grid);
var generateReportButton = new Button()
{
Text = "Generate Report"
};
generateReportButton.Clicked += new EventHandler(GenerateReport_OnClicked);
stackLayout.Children.Add(generateReportButton);
stackLayout.HorizontalOptions = LayoutOptions.Fill;
stackLayout.VerticalOptions = LayoutOptions.Fill;
scrollView.HorizontalOptions = LayoutOptions.Fill;
scrollView.VerticalOptions = LayoutOptions.Fill;
scrollView.Content = stackLayout;
absoluteLayout.Layout(new Rectangle(0,0, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
AbsoluteLayout.SetLayoutFlags(scrollView, AbsoluteLayoutFlags.PositionProportional);
absoluteLayout.Children.Add(scrollView);
absoluteLayout.HorizontalOptions = LayoutOptions.Fill;
absoluteLayout.VerticalOptions = LayoutOptions.Fill;
Content = absoluteLayout;
}
当我按下报告页面上的后退按钮时,我会看到纵向的条件页面,但是宽度比设备屏幕大得多,我只能看到前两列/第三列不可见/
我对“条件”活动有如下属性:
[Activity(Label = "Report Conditions", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
关于报告活动:
[Activity(Label = "Report", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Landscape)]
public class ConditionsCustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
//private ScreenOrientation _previousOrientation = ScreenOrientation.Unspecified;
protected override void OnWindowVisibilityChanged(ViewStates visibility)
{
base.OnWindowVisibilityChanged(visibility);
var activity = (Activity)Context;
if (visibility == ViewStates.Gone)
{
// Revert to previous orientation
activity.RequestedOrientation = ScreenOrientation.Portrait;
//_previousOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _previousOrientation;
}
else if (visibility == ViewStates.Visible)
{
//if (_previousOrientation == ScreenOrientation.Unspecified)
//{
// _previousOrientation = activity.RequestedOrientation;
//}
activity.RequestedOrientation = ScreenOrientation.Portrait;
}
}
}
public class ReportCustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
private ScreenOrientation _previousOrientation = ScreenOrientation.Unspecified;
protected override void OnWindowVisibilityChanged(ViewStates visibility)
{
base.OnWindowVisibilityChanged(visibility);
var activity = (Activity)Context;
if (visibility == ViewStates.Gone)
{
// Revert to previous orientation
activity.RequestedOrientation = _previousOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _previousOrientation;
}
else if (visibility == ViewStates.Visible)
{
if (_previousOrientation == ScreenOrientation.Unspecified)
{
_previousOrientation = activity.RequestedOrientation;
}
activity.RequestedOrientation = ScreenOrientation.Landscape;
}
}
}
此外,我还添加了自定义页面渲染器,如下所示:
[Activity(Label = "Report Conditions", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
对于条件活动:
[Activity(Label = "Report", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Landscape)]
public class ConditionsCustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
//private ScreenOrientation _previousOrientation = ScreenOrientation.Unspecified;
protected override void OnWindowVisibilityChanged(ViewStates visibility)
{
base.OnWindowVisibilityChanged(visibility);
var activity = (Activity)Context;
if (visibility == ViewStates.Gone)
{
// Revert to previous orientation
activity.RequestedOrientation = ScreenOrientation.Portrait;
//_previousOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _previousOrientation;
}
else if (visibility == ViewStates.Visible)
{
//if (_previousOrientation == ScreenOrientation.Unspecified)
//{
// _previousOrientation = activity.RequestedOrientation;
//}
activity.RequestedOrientation = ScreenOrientation.Portrait;
}
}
}
public class ReportCustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
private ScreenOrientation _previousOrientation = ScreenOrientation.Unspecified;
protected override void OnWindowVisibilityChanged(ViewStates visibility)
{
base.OnWindowVisibilityChanged(visibility);
var activity = (Activity)Context;
if (visibility == ViewStates.Gone)
{
// Revert to previous orientation
activity.RequestedOrientation = _previousOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _previousOrientation;
}
else if (visibility == ViewStates.Visible)
{
if (_previousOrientation == ScreenOrientation.Unspecified)
{
_previousOrientation = activity.RequestedOrientation;
}
activity.RequestedOrientation = ScreenOrientation.Landscape;
}
}
}
关于报告活动:
[Activity(Label = "Report", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Landscape)]
public class ConditionsCustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
//private ScreenOrientation _previousOrientation = ScreenOrientation.Unspecified;
protected override void OnWindowVisibilityChanged(ViewStates visibility)
{
base.OnWindowVisibilityChanged(visibility);
var activity = (Activity)Context;
if (visibility == ViewStates.Gone)
{
// Revert to previous orientation
activity.RequestedOrientation = ScreenOrientation.Portrait;
//_previousOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _previousOrientation;
}
else if (visibility == ViewStates.Visible)
{
//if (_previousOrientation == ScreenOrientation.Unspecified)
//{
// _previousOrientation = activity.RequestedOrientation;
//}
activity.RequestedOrientation = ScreenOrientation.Portrait;
}
}
}
public class ReportCustomContentPageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
private ScreenOrientation _previousOrientation = ScreenOrientation.Unspecified;
protected override void OnWindowVisibilityChanged(ViewStates visibility)
{
base.OnWindowVisibilityChanged(visibility);
var activity = (Activity)Context;
if (visibility == ViewStates.Gone)
{
// Revert to previous orientation
activity.RequestedOrientation = _previousOrientation == ScreenOrientation.Unspecified ? ScreenOrientation.Portrait : _previousOrientation;
}
else if (visibility == ViewStates.Visible)
{
if (_previousOrientation == ScreenOrientation.Unspecified)
{
_previousOrientation = activity.RequestedOrientation;
}
activity.RequestedOrientation = ScreenOrientation.Landscape;
}
}
}
我遗漏了一些东西,但我不确定到底是什么
非常感谢您的帮助 如果您使用
ConfigurationChanges=ConfigChanges.ScreenSize | ConfigChanges.Orientation
,则会忽略“布局区域”中的xml,如果您在“布局区域”中为景观创建了不同的xml,则不要使用此标记。感谢您的评论@GraceFeng MSFT。我想问题不在于布局,因为条件页面总是纵向的。但是,页面的宽度超出了屏幕大小。有没有办法将页面宽度设置为设备屏幕的宽度?