Layout weigth does not work Android

Asked

Viewed 57 times

0

I’m willing to leave mine <button/> and <ImageButton/> with the same size "same weight" but the command android:layout_weight="1" does not work like this leaving the size of each different.

inserir a descrição da imagem aqui

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"


tools:context="com.example.tulio.exercicio3.MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="RGM:XXXX"
    android:gravity="center"
    android:textColor="@android:color/darker_gray"
    android:textSize="24sp"
    android:typeface="normal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Fulann"
    android:gravity="center"
    android:textColor="@android:color/darker_gray"
    android:textSize="24sp"
    android:typeface="normal"
    android:id="@+id/txt"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_below="@+id/textView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="12dp"
    android:layout_marginEnd="12dp" />


<Button
    android:layout_margin="8dp"
    android:id="@+id/button"
    android:layout_width="100dp"
    android:layout_height="95dp"
    android:layout_weight="1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
  android:onClick="principal"
    android:layout_below="@+id/txt"
    android:background="@android:color/holo_blue_bright"
    android:text="Principal" />

<ImageButton
    android:layout_margin="5dp"
    android:layout_weight="1"
    android:id="@+id/button2"
    android:layout_width="100dp"
    android:layout_height="95dp"
    android:layout_below="@+id/txt"
    android:layout_toEndOf="@+id/button"
    android:layout_toRightOf="@+id/button"
    android:height="20dp"
    android:background="@android:color/darker_gray"
    android:gravity="center_vertical"
    android:src="@drawable/auto"
    android:onClick="automoveis"
    android:text="Automoveis" />

<ImageButton
    android:layout_margin="5dp"
    android:id="@+id/button3"
    android:layout_width="100dp"
    android:layout_height="95dp"
    android:layout_below="@+id/txt"
    android:layout_toRightOf="@+id/button2"
    android:background="@android:color/darker_gray"
    android:gravity="center_vertical"
    android:src="@drawable/portateis"
    android:onClick="portateis"
    android:layout_weight="1"
    android:text="Portáties" />

<ImageButton
    android:id="@+id/button4"
    android:layout_width="100dp"
    android:layout_height="95dp"
    android:background="@android:color/darker_gray"
    android:src="@drawable/empresarial"
    android:text="Empresarial"
    android:layout_weight="1"
    android:layout_below="@+id/button"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button" />

<ImageButton
    android:layout_margin="5dp"
    android:id="@+id/button5"
    android:layout_weight="1"
    android:layout_width="100dp"
    android:layout_height="95dp"
    android:text="Residencial"
    android:background="@android:color/darker_gray"
    android:src="@drawable/residencial"
    android:layout_below="@+id/button2"
    android:layout_toRightOf="@+id/button"
    android:layout_toEndOf="@+id/button" />

<ImageButton
    android:layout_margin="5dp"
    android:id="@+id/button6"
    android:layout_width="100dp"
    android:layout_height="95dp"
    android:text="Viagem"
    android:layout_weight="1"
    android:background="@android:color/darker_gray"
    android:src="@drawable/travel"
    android:layout_below="@+id/button3"
    android:layout_toRightOf="@+id/button2"
    android:layout_toEndOf="@+id/button2" />


    </RelativeLayout>
  • 1

    So, like... try to paste a fractional value, for example 5 buttons = 1/5 = 0.20... 6 buttons = 1/6 = 0.16667... and so on understood ?

  • or use a Gridlayout that your problems will be with the size will be solved, therefore Voce adjusts the span, height, width etc of the grid and all components will be inside the columns and rows of that grid

1 answer

3

This is because you specified a size for each View. Basically, when you assign a weight to a View you cannot assign a size to it, ie if you want the width or height carry a weight, the value of each of these must be 0.

<ImageButton
    android:layout_width="100dp"
    android:layout_height="0dp"
    android:layout_weight="1"

In the above case you want the height to have a weight, then it needs to be 0. If it was the width, this would be zero.

Do this in all others and your problem will be solved.

Browser other questions tagged

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