我正在制作一个解决sudokus的应用程序,所以在应用程序中我使用了一个Imageview,它显示了一个空的9x9网格,我已经放置在相对布局中。我正在尝试以编程方式创建界面,所以我想知道的是如何在Imageview顶部放置标签,以便它们匹配网格方块的位置。我知道标签可以在Imageview之上,但不知道如何将它们放到适当的位置。 Xamarin是否提供了实现此目的的方法,因为我发现的是如何使用对齐方法和相对布局。
到目前为止,这是我的代码:
public class GridActivity : Activity
{
// Global Variables
Button startButton;
Button nextButton;
RelativeLayout.LayoutParams startButtonParamsPortrait;
RelativeLayout.LayoutParams startButtonParamsLandscape;
RelativeLayout.LayoutParams nextButtonParamsPortrait;
RelativeLayout.LayoutParams nextButtonParamsLandscape;
protected override void OnCreate(Bundle savedInstanceState)
{
// Don't want the action bar to show, find it unnecessary, so gonna hide it.
ActionBar.Hide();
base.OnCreate(savedInstanceState);
// Create your application here
// get the initial orientation
var surfaceOrientation = WindowManager.DefaultDisplay.Rotation;
RelativeLayout layoutBase = new RelativeLayout(this);
layoutBase.LayoutParameters = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
layoutBase.SetPadding(100, 100, 100, 100);
// Adding a Imageview to display the sudoku grid
ImageView grid = new ImageView(this);
grid.LayoutParameters = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
grid.Visibility = ViewStates.Visible;
grid.SetBackgroundResource(Resource.Drawable.SudokuGrid);
grid.Id = 1;
layoutBase.AddView(grid);
// Adding a button that will be used to step through the "AI"'s solution
nextButton = new Button(this) { Text = "Next" };
nextButton.Id = 2;
// Adding a button that will be used to start the "AI" to solve the puzzle
startButton = new Button(this) { Text = "Start" };
startButton.Id = 3;
// Layout Parameters for Portrait mode
// nextButton
nextButtonParamsPortrait = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
nextButtonParamsPortrait.AddRule(LayoutRules.AlignParentBottom);
nextButtonParamsPortrait.AddRule(LayoutRules.AlignParentRight);
// startButton
startButtonParamsPortrait = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
startButtonParamsPortrait.AddRule(LayoutRules.AlignParentBottom);
startButtonParamsPortrait.AddRule(LayoutRules.LeftOf, nextButton.Id);
// Layout Parameters for Landscape mode
// startButton
startButtonParamsLandscape = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
startButtonParamsLandscape.AddRule(LayoutRules.AlignParentRight);
// nextButton
nextButtonParamsLandscape = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
nextButtonParamsLandscape.AddRule(LayoutRules.AlignParentRight);
nextButtonParamsLandscape.AddRule(LayoutRules.Below, startButton.Id);
// Add labels in the location of the squares
// Depending on the initial orientation, the buttons will placed at different locations
if (surfaceOrientation == SurfaceOrientation.Rotation0 || surfaceOrientation == SurfaceOrientation.Rotation180)
{
// The screen is in Portrait mode
startButton.LayoutParameters = startButtonParamsPortrait;
nextButton.LayoutParameters = nextButtonParamsPortrait;
}
else
{
// The screen is in Landscape mode
startButton.LayoutParameters = startButtonParamsLandscape;
nextButton.LayoutParameters = nextButtonParamsLandscape;
}
// Add the buttons to the layout
layoutBase.AddView(startButton);
layoutBase.AddView(nextButton);
// Display the layout to the screen
SetContentView(layoutBase);
}
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait)
{
startButton.LayoutParameters = startButtonParamsPortrait;
nextButton.LayoutParameters = nextButtonParamsPortrait;
}
else if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
{
startButton.LayoutParameters = startButtonParamsLandscape;
nextButton.LayoutParameters = nextButtonParamsLandscape;
}
}
答案 0 :(得分:0)
您可以使用绝对布局将标签放置在布局的其他子项(您的网格)上(必须是年龄较大的孩子)。
答案 1 :(得分:-1)