-1

我创建了一个测试 MvxFrameLayout 派生类,我想在 0,0 处绘制一个大小为 24x24 的孩子:

    public class MyCustomLayout : MvxFrameLayout
    {
        public MyCustomLayout(Context context, IAttributeSet attrs) : base(context, attrs)
        {
        }

        public MyCustomLayout(Context context, IAttributeSet attrs, IMvxAdapterWithChangedEvent adapter) : base(context, attrs)
        {
        }

        protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
        {
            if (!changed)
            {
                return;
            }
            for (int i = 0; i < this.ChildCount; ++i)
            {
                var child = this.GetChildAt(i);
                child.Layout(0, 0, 24, 24);
            }
        }
    }

在这样的活动布局(FirstView.axml)中使用它:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <hotspotappandroid.droid.views.MyCustomLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        local:MvxBind="ItemsSource Hotspots"
        local:MvxItemTemplate="@layout/hotspot" />
</FrameLayout>

具有一项的视图模型:

public class FirstViewModel : MvxViewModel
{
    public string[] Hotspots { get; private set; }

    public FirstViewModel()
    {
        this.Hotspots = new string[] { "A" };
    }
}

hotspot.xml 是一个 ImageView,其图像 (circle.png) 为 24x24:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/circle" />

问题是没有绘制圆形图像。

如果在hotspot.xml 中我将android:layout_width="fill_parent"and更改android:layout_height="fill_parentwrap_content,则图像被绘制,但不正确。图像被画成两半。看起来图像被缩放到其大小的两倍,并且被裁剪了一半(可能是由于`child.Layout(0, 0, 24, 24))。

我不确定发生了什么。我看到childinOnLayout是类型cirrious.mvvmcross.binding.droid.views.MvxListItemView而不是“ImageView”,因为这是我所预料的。也许这有关系?

4

1 回答 1

0

MvxFrameLayout作品通过MvxListItemView为每个孩子充气一个孩子。它MvxListItemView本身继承自FrameLayout并具有默认布局参数。

根据文档 - http://developer.android.com/reference/android/widget/FrameLayout.html - 这意味着每个子 FrameLayout 应该调整大小:

FrameLayout 的大小是其最大子项(加上填充)的大小,可见与否(如果 FrameLayout 的父项允许)

由于您在这里指定fill_parentImageView内部孙辈,那么我想这就是导致您的内部视图为零的原因。

为了给您的子框架一些尺寸,最好ImageView直接在内部子 XML 中给它们尺寸,而不是请求fill_parent


如果您确实想ImageView直接返回 an 并将其用作 Child - 没有中间体MvxListItemView,那么在最新的 MvvmCross 源代码中,我相信您可以:

于 2013-11-13T14:10:33.430 回答