Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to fill parent minus a fixed number in Android RelativeLayout?

I have an ImageButton that I want to fill its parent RelativeLayout container for its width but minus a few dp so it has some left and right padding. I tried fill_parent-10dp but that causes an error and doesn't render.

like image 364
Anthony Frizalone Avatar asked Jun 23 '12 02:06

Anthony Frizalone


People also ask

What is the difference between Fill parent and match parent?

Functionally no difference, Google just changed the name from fill_parent to match_parent, from API level 8 (Android 2.2).

What does Match_parent mean in Android?

MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. Introduced in API Level 8.

What is the difference between Match_parent and Wrap_content?

fill_parent and match_parent are the same, used when we want the height or width of a view to be as big as its parent view, fill_parent being deprecated. wrap_content is used when we want the view to occupy only as much space as required by it.

How do I create a View Center in RelativeLayout?

Below that, the layout_height=0 and layout_weight=1 attributes on the RelativeLayout cause it to take up all the remaining space. You can then center the button in the RelativeLayout . You can play with padding on the button to get it to the size you want.


2 Answers

Put a android:layout_margin="10dp" on the ImageButton, along with the

android:layout_width="fill_parent"
android:layout_height="fill_parent"
like image 180
you786 Avatar answered Oct 17 '22 07:10

you786


  1. Use the xml attribute android:layout_marginLeft to specifies extra space on the left side of your view.
  2. Use the xml attribute android:layout_marginRight to specifies extra space on the right side of your view.
  3. Use the xml attribute android:layout_marginTopto specifies extra space on the top side of your view.
  4. Use the xml attribute android:layout_marginBottom to specifies extra space on the bottom side of your view.
  5. Use the xml attribute android:layout_margin to specifies extra space on the left, top, right and bottom sides of your view.

In your case your ImageButton declaration look like this

<ImageButton
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Button" 
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:src="@drawable/a"
like image 10
K_Anas Avatar answered Oct 17 '22 05:10

K_Anas