【问题标题】:BoxView matrix in XamarinXamarin 中的 BoxView 矩阵
【发布时间】:2018-02-28 17:21:42
【问题描述】:

我目前正在为我的项目而苦苦挣扎。这个想法是制作几个可变长度的单行矩阵。在这些矩阵中,我需要根据输入更改每个 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 个这样的矩阵 [whatever x 1] 以及 2 个列 [1 xwhatever]。

我是 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.

【问题讨论】:

  • 你的问题和代码让我很困惑,你想要AbsoluteLayout 中的 16 * 2 矩形矩阵吗?
  • 不,我想制作像 8x1,然后是 5x1,然后是 6x1 的矩阵......这样会有 16 行,我已经编辑了这个问题,所以希望它能让事情变得更多清除
  • 嗨,你能告诉我boxWidthboxHeight吗?他们是 int 吗?像 10 或 5?如果是这样,请将您的AbsoluteLayoutFlags.All 更改为AbsoluteLayoutFlags.PositionProportional
  • 只是编辑,它就在那里。问题是当我使用比例时,一切都消失了,屏幕上什么也没有。无论如何,非常感谢您的努力
  • 我认为你需要阅读AbsoluteLayoutFlags,你测试过我的代码吗?那张照片是你想要的结果吗?

标签: c# android android-layout xamarin xamarin.android


【解决方案1】:

Here 有多种布局供您选择,您可以使用任何布局来实现您的目标,包括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;
    }

}

结果:

【讨论】:

  • 谢谢伙计,我刚刚完成了我需要的工作,你成为了我的个人英雄哈哈,不幸的是,我对你的投票无法计算。我想我需要努力提高自己的声誉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多