Layout weigth does not work Android

0

I'm willing to let my <button/> and%% <ImageButton/> the same size "equal weight" but the command android:layout_weight="1" does not work like leaving the size of each different.

<?xmlversion="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>
    
asked by anonymous 30.08.2017 / 17:41

1 answer

3

This is because you have specified a size for each View . Basically, when you assign a weight to a View you can not assign a size to it, that is, if you want width or height to have a weight, the value of each of these needs be equal to 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 were the width, this would be zero .

Do this on everyone else and your problem will be solved.

    
30.08.2017 / 17:47