align Linearlayout

0

I have three LinearLayout inside a LinearLayout

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/l1"
            android:layout_alignParentTop="true"></LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/l2"
            android:layout_alignParentTop="true"></LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/l3"
            android:layout_alignParentTop="true"></LinearLayout>
    </LinearLayout>

And I'm putting a ImageButton inside each of the layout as well

if(l==1){
                layout1.addView(btCategoria);
                layout1.addView(txtCategoria);
                Log.e("l1","entrou "+txtCategoria.getText());
                l=2;
            }else if(l==2){
                layout2.addView(btCategoria);
                layout2.addView(txtCategoria);
                Log.e("l2","entrou "+txtCategoria.getText());
                l=3;
            }else if(l==3){
                layout3.addView(btCategoria);
                layout3.addView(txtCategoria);
                Log.e("l3","entrou "+txtCategoria.getText());
                l=1;
            }

btCategoria and ImageButton and txtCategoria and TextView

plus the first one is not staying on the same line as the rest

Iwantedtoknowwhythefirstimageisabovetheothersnext

Withthisgridviewwillworkmoreithasthisandroid:columnWidth="200dp" , wanted to know if you can not define it because if you put a specific size will get bad depending on the cell phone né.

<GridView
    android:id="@+id/grid"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_below="@+id/spinner_widget"
    android:cacheColorHint="@color/abc_search_url_text_holo"
    android:columnWidth="200dp"
    android:gravity="center"
    android:numColumns="3"
    android:stretchMode="spacingWidth" />
    
asked by anonymous 05.03.2015 / 16:57

1 answer

0

You can set the size of each item of your GridView by calculating by the size of the device (inside your adapter):

int size;

//Recuperando a quantidade de colunas que você declarou para seu GridView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    size = getColumnSize();
} else {
    try {
        Field field = seuGridView.getClass().getDeclaredField("mColumnWidth");
        field.setAccessible(true);
        size = field.getInt(seuGridView);
    } catch (Exception ignored) {
        size = 0;
    }
}

// Fazendo o calculo para definir o melhor tamanho para cada item do seu GridView.
if (size <= 0) {
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    size = Math.round((float) 200 * density);
}

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size, RelativeLayout.LayoutParams.WRAP_CONTENT);
//definindo a largura máxima para sua imagem (ou em todo o seu layout)
suaImagem.setLayoutParams(params);

Do not forget to add the scaleType and adjusteViewBounds attribute to your ImageView :

<ImageView
    android:id="@+id/imgPhotoGalleryItem"
    android:layout_width="wrap_content"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    android:layout_height="wrap_content"/>
    
05.03.2015 / 19:50