align Linearlayout

Asked

Viewed 773 times

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 one ImageButton within each of the layout so

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;
            }

btCategoriaand the ImageButton and the txtCategoria and a TextView

but the first is not getting on the same straight as the rest

inserir a descrição da imagem aqui

wanted to know why the first image is above the other next

with this gridview will work more he has it android:columnWidth="200dp", I wanted to know if there is no way to define it because if you put a specific size will be bad depending on the cell right .

<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" />
  • Ever tried to use android:gravity="top" in the layout containing the 3?

  • 1

    Ilgner, does a Gridview not suit your layout better than mounting that Grid in hand?

  • and that the grid does not work for every type of cell phone

  • I highly recommend you use a GridView and let the system control the best way to present the images/information. And, in addition, you will have more control of the layout of each item on your grid. The GridView is available from API 1 http://developer.android.com/guide/topics/ui/layout/gridview.html and has several examples of how to implement it!

  • go take a look , and that the grid I tried didn’t work

  • I made a change in the question , because there is an item there that goes from the problem depending on the mobile , It may get too big or even too small

Show 1 more comment

1 answer

0


You can set the size of each item in your GridView calculating by device size (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);

Don’t forget to add the attribute scaleType and adjusteViewBounds in his ImageView:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.