Skip to content Skip to sidebar Skip to footer

Trying To Get Android Linearlayout To Stretch Horizontally And Vertically

UPDATE: I appreciate the advice to use GridView, and I do believe that would provide better performance. However, my initial efforts at implementing a simple GridView have result

Solution 1:

I disagree with others saying you must use a GridLayout. Try this approach with a LinearLayout. I used a generic View for each cell of the grid, but any control will work.

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal" ><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@color/red" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@color/red" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal" ><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@color/red" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal" ><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@color/red" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@color/red" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal" ><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@color/red" /><Viewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /></LinearLayout></LinearLayout>

enter image description here

This approach also requires no manual manipulation of the layout in code, everything is handled in the xml using weights.

Solution 2:

you have to try this.

<ImageViewandroid:id="@+id/iv_clm1"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/yellow" /><ImageViewandroid:id="@+id/iv_clm1"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/red" /></LinearLayout><!-- second Row --><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="0dip"android:layout_weight="1"android:orientation="horizontal" ><ImageViewandroid:id="@+id/iv_clm1"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/red" /><ImageViewandroid:id="@+id/iv_clm2"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/yellow" /><ImageViewandroid:id="@+id/iv_clm3"android:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/red" /></LinearLayout>

Solution 3:

In general, it is not a good idea to use so many Layouts. You should use TableLayout or GridLayout instead.

GridLayout:

TableLayout:

Solution 4:

As I mentioned in my UPDATE above, the real trick here is setting dimensions somewhere other than OnCreate. I initialized player1, player2, and position[] as the LinearLayout elements from the xml, then in onWindowFocusChanged I adjusted their height to fit. Not as good as GridView as suggested by others here, but its something I can actually make work!

with help from: http://www.rapidsnail.com/developer/topic/2011/222/27/35613/title-bar-and-get-out-of-the-view-of-the-status-bar-display-area-height.aspx

@OverridepublicvoidonWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            Viewcontent= getWindow().findViewById(Window.ID_ANDROID_CONTENT);
            player1.getLayoutParams().height=content.getHeight()/6;
            player2.getLayoutParams().height=content.getHeight()/6;
            for (int i=0;i<12;i++){
                    position[i].getLayoutParams().height=content.getHeight()/6;
            }

Post a Comment for "Trying To Get Android Linearlayout To Stretch Horizontally And Vertically"