0

I want to make a layout in xml for a Fragment class. This layout have to be match_parent width and height.

Picture 1

enter image description here

In picture 1, the layout has four items added programatically...

Picture 2

enter image description here

In picture 2 is the same layout but with 6 items.

none of these layouts examples are scrollable, so if i add 8 items, all items has to fit to the screen size.

how can i do this?

Community
  • 1
  • 1
andrescabana86
  • 1,778
  • 8
  • 30
  • 56

3 Answers3

0

You can uses ScrollView.
if you use GridView now, ScrollView is contain GridView.
Or You use over scroll mode if ifContentScrolls mode.

Amadas
  • 703
  • 1
  • 5
  • 10
0

You can do this using layout_weight attribute in LinearLayout as below...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:text="Menu 1" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:text="Menu 2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:text="Menu 1" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:text="Menu 2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:text="Menu 3" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:text="Menu 4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:text="Menu 5" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:text="Menu 6" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:text="Menu 7" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:text="Menu 8" />
    </LinearLayout>

</LinearLayout>
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
0

You can do it simply: refer to the LinearLayout by id, and add children programatically. If you want its children to have the same height, set their height to 0px and their weight to the same, 1 for instance. Since its children will be Linearlayouts as well, you can add the items manu1, menu2, etc to them without problem. I don't know you exact usage, but it's a start.

Ripityom
  • 426
  • 1
  • 5
  • 12