简体   繁体   English

Android:如何创建这种动态布局?

[英]Android: how to create this kind of dynamic layout?

My aim is to display people/addresses in a list - but not just a simple list, rather a list of boxes that contain several information, in my case a number, the address and a type: 我的目的是在列表中显示人员/地址-而不只是一个简单的列表,而是一个包含多个信息的框列表,在我的情况下是一个数字,地址和类型:

在此处输入图片说明

The data structure for this are POJOs: 为此的数据结构是POJO:

  • MyPojo.java (Class) MyPojo.java(类)
  • + getNumer() + getNumer()
  • + getType() + getType()
  • + getName() + getName()
  • + getStreet() + getStreet()
  • + getCity() + getCity()

Of course, there are more than 4 entries which means that there should be a scrollbar. 当然,有四个以上的条目,这意味着应该有一个滚动条。 My first idea: a Scrollview that contains a Linear Layout . 我的第一个想法:包含Linear LayoutScrollview But I'm not sure about the content, means the boxes you can see above. 但我不确定内容,即您可以在上方看到的方框。

  1. How can I define such boxes that represent the list items? 如何定义代表列表项的框? As you can see above, a box consists of three parts that are divided from each other (Number, Type, Address). 如上所示,一个盒子由三个部分组成,这三个部分彼此分开(数字,类型,地址)。
  2. Of course, it must be possible to add and remove boxes (means: entries) dynamically at runtime to the Linear Layout. 当然,必须有可能在运行时动态地向线性布局添加和删除框(均值:条目)。

Thank you 谢谢

My first idea: a Scrollview that contains a Linear Layout.

Are you familiar with a listView ? 您熟悉listView吗? This already exists to do exactly that. 确实已经存在做到这一点。 I'm sure you can find dozens of tutorials on how to use them by searching. 我相信您可以通过搜索找到许多有关如何使用它们的教程。

  • Create a ListActivity class 创建一个ListActivity
  • Create a separate layout file for your item 为您的项目创建一个单独的布局文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" > <LinearLayout android:orientation="vertical" > <TextView /> <TextView /> </LinearLayout> </TextView> </LinearLayout>

  • Then Create an inner class in ListActivity which extends from ArrayAdapter . 然后在ListActivity创建一个内部类,该内部类从ArrayAdapter扩展。 In the getView() method, use LayoutInflater to inflate yourItemLayout and update the TextView based on the position values. getView()方法中,使用LayoutInflater膨胀yourItemLayout并根据position值更新TextView。
  • From onCreate in the main class, call setListAdapter(new InnerClass(this,R.layout.yourItemLayout,list) 从主类的onCreate调用setListAdapter(new InnerClass(this,R.layout.yourItemLayout,list)

You can refer customArrayAdapter to know how to custom ArrayAdapter works. 您可以参考customArrayAdapter来了解如何自定义ArrayAdapter。

Done and dusted. 尘埃落定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM