【发布时间】:2015-10-30 17:07:43
【问题描述】:
我正在开发一个项目,我正在使用 XML 视图的 Visual Studio 2015 的 Xamarin.Android 插件。我需要为应用程序创建一个屏幕,其中 ImageButtons 的布局如下图所示,但我需要根据可以更改图像按钮显示内容的列表动态创建它。最终结果看起来像图像,但根据列表中的内容可能会出现更少的按钮。我不太确定如何解决这个问题,因为我在 xml 中使用 GridViews 的次数要少得多。所以基本上在代码中我到目前为止得到的只是填充列表:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MainMenu);
List<User> configList = new List<User>(user.Configurations);
}
xml布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="@+id/LocationsRoot"
android:gravity="center_vertical|center_horizontal">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_below="@id/toolbar">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/space2"
android:layout_marginBottom="5dp"
android:layout_weight="1" />
<include
android:id="@+id/bottomtoolbar"
layout="@layout/toolbarbottom"
android:layout_weight="0" />
</LinearLayout>
</RelativeLayout>
所以每个图像按钮都会有不同的图像。图片:
如何以编程方式实现这一目标?
【问题讨论】:
-
最终,您想要一个动态构建的网格视图,每个按钮下都有一些文本?
-
是的,我需要遍历我的 onCreate 方法中显示的列表才能构建这些图像按钮。
-
可能希望将其用作参考指南 (code.tutsplus.com/tutorials/…),是的,它在 Java 中,但到 C# 的翻译是直截了当的。
-
但这并不能真正满足它的动态部分......所以列表可能只包含其中两个按钮,但有时列表可能包含这些按钮的所有八个对象...... .
-
是的,遍历您的列表,然后添加新的图像按钮
标签: c# android gridview xamarin android-imagebutton