I'm using Relative Layout to create a calculator screen, and i have a row of buttons labeled 1, 2, and 3. I want them evenly spaced, but i'm not sure how to do this with Relative Layout
I'm used to using the android:layout_weight function in LinearLayout, and give each of them a value of android:layout_width="fill_parent", as well as a layout_weight="1"
any way to get this to happen on calculator screen (without specifying DP or PX)
this is what i have set up at the moment, and they are aligned in order from left to right, under the TextView (calculator output screen). any suggestions/solutions to evenly space, filling the width of the screen?
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_text"
android:layout_margin="6px"
/>
<Button
android:id="@+id/button1"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
/>
<Button
android:id="@+id/button2"
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_toRightOf="@+id/button1"/>
<Button
android:id="@+id/button3"
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_toRightOf="@+id/button2"
/>
Relative layout: image centered, minWidth set, texts have set toRightOf/toLeftOf. Relative layout: image has set toRightOf/toLeftOf. Constraint layout: "constraint arrows" go from image to text. Constraint layout: "constraint arrows" go from text to image.
@filthy_wizard: RelativeLayout is not deprecated. PercentRelativeLayout is deprecated.
RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).
You are looking for the android:layout_weight attribute. It will allow you to use percentages to define your layout. In the following example, the left button uses 70% of the space, and the right button 30%. It works the same with any kind of View, you can replace the buttons with some EditText to fit your needs.
If you want equal spacing, you do not want a RelativeLayout
. Use a LinearLayout
, as you suggest in your question. If needed, put the Buttons
in a LinearLayout
and put the LinearLayout
in the RelativeLayout
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With