user3244162
user3244162

Reputation: 101

Android Grid Layout display

I use a GridLayout in an Android application but I have a display problem,

I used setColumnCount to have 3 column because I have to add 3 elements per line, so it should be aligned.

layout1 = new GridLayout(this);
layout1.setColumnCount(3);

 //In a loop later in the code :
layout1.addView(textView1);
layout1.addView(cb);
layout1.addView(textView2);

3 items are not aligned with the lines, but they are all in the first column, I do not really understand the problem.

Upvotes: 1

Views: 261

Answers (4)

Mehul Ranpara
Mehul Ranpara

Reputation: 4255

Have you tried this? :

<GridView
        android:id="@+id/photo_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       android:layout_margin="10dp"
        android:numColumns="3" >

</GridView>

And make 1 custom xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    >

    <ImageView
        android:id="@+id/img_photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:layout_margin="5dp"
        android:src="@drawable/img_photo_caption" />

</LinearLayout>

Upvotes: 0

J&#233;remy
J&#233;remy

Reputation: 53

Maybe if it's static and it will be always be 3 items you can set a LinearLayout in the xml with orientation horizontal.

Upvotes: 0

ray6080
ray6080

Reputation: 893

You should specify which cell adds which view in your addView method. This doc about GridLayout.addView may help you.

Upvotes: 0

Thanh Le
Thanh Le

Reputation: 773

Using LayoutParams for your child view

GridLayout gridLayout = new GridLayout(this);
gridLayout.setColumnCount(colCount);
gridLayout.setRowCount(rowCount);

GridLayout.LayoutParams third = new GridLayout.LayoutParams(0, 0);
textView1.setLayoutParams(third);
gridLayout.addView(textView1, third);

GridLayout.LayoutParams fourth = new GridLayout.LayoutParams(0, 1);
cb.setLayoutParams(fourth);
gridLayout.addView(cb, fourth);

GridLayout.LayoutParams fifth = new GridLayout.LayoutParams(0, 2);
textView2.setLayoutParams(fifth );
gridLayout.addView(textView2, fifth);

Upvotes: 1

Related Questions