我正在尝试在平板电脑上以相对布局布置 View ,就像书架一样。会有很多交叉,然后创建一个新行。
我的初始状态是这样的:
<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 次,然后换行。
我最初的想法是使用布局充气器并以编程方式添加 View ,使用 RelativeLayout.LayoutParams 设置 layout_toRightOf 值。
但一定有更简单的方法。我见过很多使用“书架”比喻的应用程序。
最佳答案
我能想到几个选项。
- 为什么不使用具有水平属性的 LinearLayouts 而不是使用相对布局?
- 使用 TableLayout,与 LinearLayout 非常相似
- 使用带有自定义适配器的 ListView 为每个 ListRow 填充五个“添加用户头像”
关于android - 以编程方式在布局中布置 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7134142/