2011-08-20 60 views
3

我试图在平板电脑上布局相对布局中的视图,非常像书架。将会有这么多,然后创建一个新的行。以编程方式在布局中布置子视图

我的初始状态是这样的:

<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,然后换行到新行。

我最初的想法是使用布局inflater并以编程方式添加视图,使用RelativeLayout.LayoutParams来设置layout_toRightOf值。

虽然必须有一个更简单的方法。我见过很多使用“书架”隐喻的应用程序。

+0

我的第一个多达五个镜像视图的水平布局以为会使用GridView。 – kcoppock

回答

3

有几个选项我可以想到。

  1. 而不是使用相对布局,为什么不使用LinearLayouts水平属性?
  2. 使用TableLayout,很像的LinearLayout
  3. 使用带有自定义适配器的ListView填补五“添加用户阿凡达”每ListRow
3

编程铺出的观点是相当简单的。我为我所有的活动/观点都这样做。我喜欢Android的XML布局概念,但发现一旦视图根据外部数据(例如数据库)完全动态变化,就会变得太复杂。

我选择我最外层的布局,然后只在XML中实例化该布局。然后在我的活动中,我使用普通的find by id call找到外部布局,然后使用一系列add(View)方法调用来根据需要添加我的动态创建的视图或布局。

您需要考虑不同的屏幕方向,像素密度和尺寸。独立于设备的像素将成为您的朋友。例如,要创建图像视图,您需要加载位图,找出需要调整大小的位置(如果需要),然后将其设置为新ImageView实例的drawable,然后将其添加到布局。

听起来像你外布局将与内垂直线性布局的滚动视图中,然后对每一个“架子”是然后对每个“书”

相关问题