gpt4 book ai didi

android - 有没有办法让 Mvx.MvxLinearLayout 尊重 android :layout_weight?

转载 作者:太空狗 更新时间:2023-10-29 12:46:09 26 4
gpt4 key购买 nike

我有一个水平项目列表,我希望这些项目平均共享可用的屏幕宽度。

以前,我对每个项目使用索引绑定(bind),并使用静态 LinearLayout,每个项目具有相同的 layout_weight。

当我用 Mvx.MvxLinearLayout 和 ItemsSource 绑定(bind)替换静态 LinearLayout,并将项目的标记移动到 Itemtemplate 时,MvxLinearLayout 不再考虑 layout_weight。

我已经尝试了各种重构项目模板的方法,但似乎没有办法让这个控件在安排其项目时服从 layout_weight。

有没有办法让 Mvx.MvxLinearLayout 遵守 android:layout_weight,或者有什么替代方法可以给我一个动态的项目列表,这些项目将在给定固定尺寸边界的情况下平均排列?

编辑

假设一个包含 3 个项目的列表,具有字符串属性“Name”。

此标记按预期工作,提供 3 个等宽的文本项:

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxBind="Text Items[0].Name"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxBind="Text Items[1].Name"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxBind="Text Items[2].Name"
/>
</LinearLayout>

此方法不起作用,布局权重未正确应用于 TextViews:

<Mvx.MvxLinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource Items"
local:MvxItemTemplate="@layout/item_template" />

ItemTemplate 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxBind="Text Name" />

最佳答案

问题是,当将 MvxLinearLayout 与 ItemTemplate 一起使用时,mvvmcross 会插入一个额外的 View 。因此,不是获取 View 层次结构:

MvxLinearLayout -> TextView

你最终得到:

MvxLinearLayout -> MvxListItemView -> TextView。

因此,应用于 TextView 的布局属性(尤其是权重)对于 LinearLayout 是不可见的。 MvxListItemView 使用默认布局属性(包装内容)进行填充,并且看不到重量效果。

Action 发生的位置在 MvxViewGroupExtensions(例如 Refill)中,其中 subview 被添加到 LinearLayout,以及 MvxAdapter 中的 CreateBindableView 方法,其中 subview 被打包在一个 IMvxListItemView 中。

解决此问题的一种方法是覆盖 MvxViewGroupExtensions 中的方法,并在添加 View 后更新 MvxListItemViews 的布局属性(例如,从底层 View 复制它们)。例如:

private static void Refill(this ViewGroup viewGroup, IAdapter adapter) {
.... // rest of code
Invalidate();
for (int j = 0; j < viewGroup.ChildCount; j++)
{
var child = GetChildAt(j) as MvxListItemView;
var param = new LinearLayout.LayoutParams(
0, ViewGroup.LayoutParams.WrapContent, 1.0f);
child.LayoutParameters = param;
}
}

关于android - 有没有办法让 Mvx.MvxLinearLayout 尊重 android :layout_weight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com