哈马姆的BoxView矩阵

问题描述 投票:0回答:1

我目前正在努力完成我的项目。我们的想法是制作一些长度可变的行矩阵。在那些矩阵中,我需要更改每个boxView的颜色取决于输入。显然,我在创建矩阵时陷入了该项目的开端。使用下面的代码,我可以创建一个或多个矩阵并在X方向上移动它,但是它无法在Y中移动。我做错了什么?

// BoxView dot dimensions.
double boxHeight = 1;
double boxWidth = 0.05;

public BoxView[,] rowPlaces(int count, double x, double y)
{
    BoxView[,] digitBoxViews = new BoxView[count, 1];

    // Create and assemble the BoxViews.
    double xIncrement = 0.035 ;
    double yIncrement = 0.03;

    for (int index = 0; index < count; index++)
    {
        for (int col = 0; col < 1; col++)
        {
            for (int row = 0; row < count; row++)
            {
                // Create the index BoxView and add to layout.
                BoxView boxView = new BoxView();
                digitBoxViews[row, col] = boxView;
                absoluteLayout.Children.Add(boxView,
                                            new Rectangle(x, y, boxWidth, boxHeight),
                                            AbsoluteLayoutFlags.All);
                digitBoxViews[row, col].Color = free;
                y += yIncrement;
            }

            x += xIncrement;
        }
        x += xIncrement;
    }
    return digitBoxViews;
}

在MainPage()中,我只是调用这个方法,正如我所提到的那样,应该有16个这样的矩阵[无论是x 1]还有2个列[1 x无论如何]。

我是Android新手,有点困惑,我觉得这个错误是基本的?也许错误的布局(目前使用absoluteLayout)

To make things more clear here is a picture of approximately layout I want to reach, each boxView should be reachable by name of matrix(vector) and it's index to change color.

c# android android-layout xamarin xamarin.android
1个回答
1
投票

Here are你可以选择很多布局,你可以使用任何布局来实现你的目标,包括AbsoluteLayout

我根据你的代码做了些什么:

Layout

<AbsoluteLayout 
    x:Name="absoluteLayout"
    BackgroundColor="Yellow">

</AbsoluteLayout>

MainPage

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        rowPlaces(5, 0.2, 0.2);
    }

    public BoxView[,] rowPlaces(int count, double x, double y)
    {
        BoxView[,] digitBoxViews = new BoxView[count, 2];
        double sourcey = y;
        // Create and assemble the BoxViews.
        double xIncrement = 0.25 / count;
        double yIncrement = 0.5 / count;

        for (int index = 0; index < count; index++)
        {
            for (int col = 0; col < 2; col++)
            {
                for (int row = 0; row < count; row++)
                {
                    // Create the index BoxView and add to layout.
                    BoxView boxView = new BoxView();
                    digitBoxViews[row, col] = boxView;
                    absoluteLayout.Children.Add(boxView,
                        new Rectangle(x, y, 10, 10),
                                                AbsoluteLayoutFlags.PositionProportional);
                    digitBoxViews[row, col].Color = Color.Blue;
                    y += yIncrement;
                }

                x += xIncrement;
                y = sourcey;
            }
            x += xIncrement;
        }
        return digitBoxViews;
    }

}

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.