Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laying out subviews in a layout programmatically

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.

like image 940
James Avatar asked Jul 09 '26 03:07

James


1 Answers

There are a few options I can think of.

  1. Instead of using a relative layout, why not use LinearLayouts with the horizontal attribute?
  2. Using a TableLayout, much like the LinearLayout
  3. Use a ListView with a custom adapter to fill five 'Add User Avatar's' per ListRow
like image 73
MrZander Avatar answered Jul 10 '26 18:07

MrZander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!