How to leave space between images?

Asked

Viewed 149 times

3

I have this layout:

inserir a descrição da imagem aqui

telainicial.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ad_linear_layout"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context="praias.android.makerapp.com.praias.TelaInicial"
android:background="#ffffff">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView"
    android:layout_above="@+id/adView">

    <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_marginRight="5dp"
            android:layout_marginTop="5dp"

            android:layout_weight="1"
            android:gravity="start"
            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_marginRight="7dp"
            android:layout_marginTop="5dp"

            android:layout_weight="1"
            android:gravity="start"
            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_marginTop="5dp"
            android:layout_weight="1"
            android:gravity="start"
            android:id="@+id/l3"
            android:layout_alignParentTop="true"></LinearLayout>
    </LinearLayout>
</ScrollView>

Here in java I create the imageview and the textview and Seto at each linearlayout telainicial.java

            if(l==1){
                layout1.addView(btCategoria);
                layout1.addView(txtCategoria);

                l=2;
            }else if(l==2){
                layout2.addView(btCategoria);
                layout2.addView(txtCategoria);

                l=3;
            }else if(l==3){
                layout3.addView(btCategoria);
                layout3.addView(txtCategoria);

                l=1;
            }

I used the android:layout_marginRight="7dp" to leave a space between the images aside. There, I was wanting to leave a space underneath for the textiew not to get too glued to the image. I tried to use the android:layout_marginTop="5dp" and also the android:layout_marginBottom="5dp" to distance a little, but neither of the two worked.

Note: I didn’t use a gridview because what I found didn’t work for my api

Creation of buttons

TextView txtCategoria = (TextView)LayoutInflater.from(this).inflate(R.layout.text, null);

            final ImageButton btCategoria = (ImageButton) LayoutInflater.from(this).inflate(R.layout.button, null);
            btCategoria.setId((int) listenerCategoria.id);

Layout statement

layout1 = (LinearLayout) findViewById(R.id.l1);
    layout2 = (LinearLayout) findViewById(R.id.l2);
    layout3 = (LinearLayout) findViewById(R.id.l3);

Resolution

txtCategory.setPadding(0,0,0,10);

put a serPadding on the textviewe put the bottum at 10

  • These views btCategoria and txtCategoria are created programmatically, without any XML?

  • yes , are created in cidigo , more I think I already know why I’m not able to leave a space with the android:layout_marginBottom="5dp", because all images and textview are within the same layout and I’m putting it in the layout so it would only work if I had a layout below, have as I set this layout_bottom in java?

1 answer

3


Just like these two views were created programmatically, you can also set the margin values like this:

LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 0, 0, 50);
txtCategoria.setLayoutParams(llp);

In this example the value of the lower margin of the TextView, thus the next inclusion of ImageView within this layout will have this space on top.

  • the way I use lauyout is not appearing this setMargins

  • What do you mean? You can use it the way it is. Show what you’ve done.

  • look up I have 3 linearlayout I gave a findviewbyid

  • the linearlayout I have already created in xml, only the image and the textview that does not

  • I got it , I’m putting it in the question what I did

  • Yes, I know, but this application is to be done exactly in this Textview. Padding works, but in practice even the right thing is to have margin. The Linearlayout of.

Show 1 more comment

Browser other questions tagged

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