How to divide a TableRow into two equal parts?

0

I am working a screen that will be a menu, there will be 4 options and I am a TableLayout and in a TableRow I have put two buttons, the situation is if there is a way to divide is TableRow in two equal parts, is that it gives the impression of have 3 "cells" Here is the code I use:

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <Button
            android:id="@+id/btnPesquisa"
            android:layout_width="250dp"
            android:layout_height="200dp"
            android:layout_column="0"
            android:fontFamily="monospace"
            android:text="Pesquisa Larvaria"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

        <Button
            android:id="@+id/btnCaptura"
            android:layout_width="250dp"
            android:layout_height="200dp"
            android:layout_column="1"
            android:fontFamily="monospace"
            android:text="Captura Adulto"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <Button
            android:id="@+id/btnGotaGrues"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:fontFamily="monospace"
            android:text="Gota Gruesa"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

        <Button
            android:id="@+id/button"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:fontFamily="monospace"
            android:text="Seguimiento a Botiquin"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
    </TableRow>

</TableLayout>
    
asked by Igmer Rodriguez 28.03.2018 в 08:22
source

2 answers

1

Use the android: weightSum attribute. For example something like this:

<TableLayout android:weightSum="2">
        <TableRow android:layout_weight="1"></TableRow>
        <TableRow android:layout_weight="1"></TableRow>
    </TableLayout>

Here is an example:

<TableLayout 
    android:weightSum="2"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <TableRow
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1" 
        android:background="@android:color/black">
    </TableRow>
    <TableRow 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_red_light">
    </TableRow>
</TableLayout>

WeightSum you can use it in most layouts. I recommend that leaving the ease of a tablelayout or Linearlayout, start using ConstraintLayout which is much more versatile and you can do this same effect quietly.

    
answered by 28.03.2018 в 09:12
1

Your layout currently looks like this:

To see the 2 segments exactly divided on the screen use android: layout_weight . , defining an equitable measure such as android:layout_weight="0.5" or android:layout_weight="1" for both views ( TableRow ).

  

You can set android: layout_weight in item views   individual secondaries to specify how the linear design divides   the remaining space between the views it contains. See the guide   Linear design to see an example.

It is important to mention that you do not need to define android: weightSum since the operating system calculates the appropriate measure.

This would be the code:

   <TableRow
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <Button
            android:id="@+id/btnPesquisa"
            android:layout_width="250dp"
            android:layout_height="200dp"
            android:layout_column="0"
            android:fontFamily="monospace"
            android:text="Pesquisa Larvaria"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

        <Button
            android:id="@+id/btnCaptura"
            android:layout_width="250dp"
            android:layout_height="200dp"
            android:layout_column="1"
            android:fontFamily="monospace"
            android:text="Captura Adulto"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
    </TableRow>

    <TableRow
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <Button
            android:id="@+id/btnGotaGrues"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:fontFamily="monospace"
            android:text="Gota Gruesa"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

        <Button
            android:id="@+id/button"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:fontFamily="monospace"
            android:text="Seguimiento a Botiquin"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
    </TableRow>

A uniform measure would be obtained in each TableRow :

    
answered by 28.03.2018 в 21:57