矩阵图像视图布局

时间:2015-12-26 20:49:14

标签: android android-layout android-fragments layout android-imageview

我正在尝试创建一个简单的记忆游戏。我的基本布局来自两个片段,一个带有卡片,另一个带有移动历史记录。但是我无法动态生成我想要的布局。

我希望卡片始终适合屏幕(如果屏幕较小,则缩放图像)。

在屏幕的另一半(或第三个)应该是带有移动历史的列表视图(没有图像,只是一些文本表示)。

所需的布局

Desired layout

我的结果 enter image description here

布局

横向和纵向布局之间的区别在于线性布局方向。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
    class="Pexeso.Fragments.CardsFragment"
    android:id="@+id/cardsFragment"
    android:layout_width="fill_parent"
    android:layout_height="380dp" />
<fragment
    class="Pexeso.Fragments.HistoryFragment"
    android:id="@+id/historyFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

矩阵生成(Xamarin - C#)

using System.Collections.Generic;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using PexesoGame.Models.Pexeso;

namespace Pexeso.Fragments
{
public class CardsFragment : Fragment
{
    public List<Card> Cards => PexesoGame.Game.Pexeso.Instance.Cards;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var rl = new RelativeLayout(Activity)
        {
            LayoutParameters = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MatchParent,
                ViewGroup.LayoutParams.WrapContent)
        };
        rl.SetGravity(GravityFlags.Center);

        int index = 1;
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                ImageView img = new ImageView(Activity)
                {
                    Id = index
                };

                img.SetImageDrawable(this.Cards[index - 1].Image);

                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.WrapContent,
                        ViewGroup.LayoutParams.WrapContent);

                rlp.SetMargins(5, 5, 5, 5);

                if (j == 0)
                {
                    //rlp.AddRule(LayoutRules.AlignParentLeft, (int)LayoutRules.True);
                }
                else
                {
                    rlp.AddRule(LayoutRules.RightOf, index - 1);
                }
                if (i == 0)
                {
                    //rlp.AddRule(LayoutRules.AlignParentTop, (int)LayoutRules.True);
                }
                else
                {
                    rlp.AddRule(LayoutRules.Below, index - 6);
                }

                img.LayoutParameters = rlp;
                img.SetAdjustViewBounds(true);
                img.SetScaleType(ImageView.ScaleType.CenterInside);
                rl.AddView(img);

                index++;
            }
        }

        //return view
        return rl;
    }
}
}

0 个答案:

没有答案