以编程方式在布局中布置子视图
我正在尝试在平板电脑上以相对布局来布局视图,就像书架一样。会有很多交叉,然后创建一个新行。
我的初始状态如下所示:
<ScrollView
android:layout_marginTop="150sp"
android:layout_marginLeft="50sp"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/user_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Add User Avatar -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/user_frame" />
<ImageView
android:id="@+id/demo_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/add_user"
android:layout_marginLeft="10px" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/user_name_background"
android:layout_marginLeft="10px" />
<TextView
android:id="@+id/user_name"
android:layout_width="180px"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center_horizontal"
android:text="Add New User" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
我正在从数据库中获取记录,对于每条记录,我需要放置一个版本的“添加用户头像”部分,如上所示。我想做 5,然后换行到新行。
我最初的想法是使用布局充气器并以编程方式添加视图,使用RelativeLayout.LayoutParams 设置layout_toRightOf 值。
但一定有一种更简单的方法。我见过很多使用“书架”比喻的应用程序。
I'm trying to layout views in a relative layout on a tablet, much like a bookshelf. There will be so many across, then a new row is created.
My initial state looks like this:
<ScrollView
android:layout_marginTop="150sp"
android:layout_marginLeft="50sp"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/user_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Add User Avatar -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/user_frame" />
<ImageView
android:id="@+id/demo_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/add_user"
android:layout_marginLeft="10px" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/user_name_background"
android:layout_marginLeft="10px" />
<TextView
android:id="@+id/user_name"
android:layout_width="180px"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center_horizontal"
android:text="Add New User" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I'm fetching records from a database, and for each record I need to place a version of the "add user avatar" section as seen above. I'd like to do 5, then wrap to a new row.
My initial idea was to use a layout inflater and add the views programmatically, using RelativeLayout.LayoutParams to set the layout_toRightOf values.
There must be an easier way though. I've seen lots of applications using the "bookshelf" metaphor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到几个选择。
There are a few options I can think of.
以编程方式布局视图相当简单。我对我的所有活动/观点都这样做。我喜欢 Android 的 XML 布局概念,但发现一旦视图基于外部数据(例如数据库)变得完全动态,那么它就会变得太复杂。
我选择最外层的布局,然后仅在 XML 中实例化该布局。然后在我的活动中,我使用正常的 find by id 调用找到外部布局,然后使用一系列 add(View) 方法调用来根据需要添加动态创建的视图或布局。
您需要考虑不同的屏幕方向、像素密度和尺寸。设备独立像素将成为您的朋友。例如,要创建图像视图,您需要加载位图,计算出要调整其大小的程度(如果需要),然后将其设置为新 ImageView 实例的可绘制对象,然后将其添加到布局中。
听起来你的外部布局将是一个滚动视图,内部有一个垂直线性布局,然后每个“书架”都有一个水平布局,每个“书”最多有五个图像视图
Laying out views programmatically is fairly simple. I do this for all of my activities / views. I love Android's XML layout concept but find that as soon as the views become at all dynamic based on external data (such as your database) then it gets too complex.
I choose what my outermost layout will be and then only instantiate that layout in the XML. Then in my activity I find the outer layout using the normal find by id call and then use a sequence of add(View) method calls to add my dynamically created views or layouts as needed.
You will need to take into account different screen orientations, pixel densities and sizes. Device Independent Pixels will become your friend. For example, to create an image view, you would load the bitmap, figure out how much to resize it (if needed) and then set it as the drawable for a new ImageView instance, that you then add to the layout.
Sounds like your outer layout will be a scroll view with a vertical linear layout inside, that then has a horizontal layout for each "shelf" that then have up to five image views for each "book"