在Android中创建一个周期表

时间:2014-09-26 22:43:28

标签: android dynamic tablelayout

对于我的应用,我需要创建一个periodic table。 开始时,我想做一个tablelayout并用特定的Views动态填充它,代表json文件中的每个元素。但是,我了解到通常动态视图是指我们不确切知道视图的数量,并且布局适应这个未知数。

但在我的情况下,我知道视图的数量以及显示它们的位置。

所以:你对我有什么建议?是否可以制作一个大约100个元素的布局? 我的问题更多地与android中的最佳实践相关,也是优化内存的最佳方式。 感谢

1 个答案:

答案 0 :(得分:0)

由于元素周期表的元素是已知的,我建议将每一行转换为一个包含TextView的LinearLayout。对于那些没有元素的地方,将其转换为空间视图或仅仅是普通视图。确保将每个TextView的宽度设置为0dp并为其指定权重;这样它们就可以均匀地分开。

最终会像

一样
<LinearLayout orientation=vertical width=match_parent height=match_parent>
    <LinearLayout orientation=horizontal width=match_parent height=wrap_content>
        <TextView width=0dp, height=wrap_content, weight=<> text=Element-Name/>
        <Space width=0dp, height=wrap_content, weight=<>/>
        <TextView width=0dp, height=wrap_content, weight=<> text=Element-Name>
    </LinearLayout>

    .. another linearlayout similar to the above one for each row of the periodic table

</LinearLayout>

另一种方法是创建自定义视图;一个视图,它考虑了包含元素的单行。

另一种方法是考虑GridView,只要你想从json文件加载内容。