'Evenly distribute elements on screen
I want to evenly distribute the elements "checkbox, desayuno,elegir hora" to the full width of the screen.After the "elegir hora" button there is a small Textview where the chosen time is shown, but very small. I have tried setting android:layout_weight="1" but it didn't work.this is how it is displayed:screenshot
Heres the layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nombre de la pastilla"
android:gravity="center|center"
/>
<EditText
android:id="@+id/nombrePastilla"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:background="@color/color_blanco"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBoxDesayuno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Desayuno"
android:layout_weight="1"
/>
<Button
android:id="@+id/buttonDesayuno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Elegir hora"
android:layout_weight="1"
/>
<TextView
android:id="@+id/horaDesayuno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=""
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBoxComida"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Comida"
/>
<Button
android:id="@+id/buttonComida"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Elegir hora" />
<TextView
android:id="@+id/horaComida"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=""
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBoxCena"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Cena" />
<Button
android:id="@+id/buttonCena"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Elegir hora" />
<TextView
android:id="@+id/horaCena"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=""
/>
</LinearLayout>
<Button
android:id="@+id/finalizar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Finalizar" />
</LinearLayout>
Solution 1:[1]
//You have to give weightsum to linear layout and layoutweight to your textview
and checkbox(Simply copy and paste this)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|center"
android:text="Nombre de la pastilla" />
<EditText
android:id="@+id/nombrePastilla"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<CheckBox
android:id="@+id/checkBoxDesayuno"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Desayuno" />
<Button
android:id="@+id/buttonDesayuno"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="Elegir hora" />
<TextView
android:id="@+id/horaDesayuno"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="start|center_vertical"
android:text="dmeo"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<CheckBox
android:id="@+id/checkBoxComida"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Comida"
/>
<Button
android:id="@+id/buttonComida"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="Elegir hora" />
<TextView
android:id="@+id/horaComida"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="start|center_vertical"
android:text="dmeo"
android:textSize="18dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<CheckBox
android:id="@+id/checkBoxCena"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Cena" />
<Button
android:id="@+id/buttonCena"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="Elegir hora" />
<TextView
android:id="@+id/horaCena"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="start|center_vertical"
android:text="dmeo"
android:textSize="18dp"
/>
</LinearLayout>
<Button
android:id="@+id/finalizar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Finalizar" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dhruv Sakariya |