Skip to content Skip to sidebar Skip to footer

Layout Dynamic Grid In Middle

I have a grid with (currently) four cells wide. Each cell I am specifying to be 20px wide. I want to position this in the middle and at the top of a portrait layout. My code is bel

Solution 1:

Try:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#ffff00">

        <View
            android:layout_width="0dp"
            android:background="#ff0000"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <GridView
            android:id="@+id/grid_view"
            android:background="#ffffff"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:numColumns="4"
            android:columnWidth="20dp"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"/>

        <View
            android:layout_width="0dp"
            android:background="#FFFF00FF"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:background="#FF00FFFF"
        android:layout_height="0dp"
        android:layout_weight="3"/>

</LinearLayout>

You will get something like that:

Example of wanted layout

BTW: you can change the width/height of your GridView as you wish, you can put there a weight too with a similar approach as for the views next to it.


Post a Comment for "Layout Dynamic Grid In Middle"