What is the function of wrap_content?

Asked

Viewed 6,645 times

1

    RelativeLayout onCreateLayout = new RelativeLayout(this);
    onCreateLayout.setBackgroundColor(Color.CYAN);

    RelativeLayout.LayoutParams ClickMeParms = new RelativeLayout.LayoutParams  (
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
            );

    ClickMeParms.addRule(RelativeLayout.CENTER_HORIZONTAL);
    ClickMeParms.addRule(RelativeLayout.CENTER_VERTICAL);

2 answers

5


The WRAP_CONTENT is a property that defines the size (height or width) of the view based on its content. This value indicates that the size of this element should fit the content assigned to it.

See an example below, where the height of LinearLayout will be proportional to the size of the view Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
>
    <Button
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Button"
 />
</LinearLayout>

In other words, it should only take up the space you need (height and/or width) to display your information in the layout.

See this little illustration below where the orange marking represents a LinearLayout property wrap_content height in left image and match_parent in the image on the right.

inserir a descrição da imagem aqui

0

Its function is to inform that the view or layout should be sized enough to show all its contents, including any paddind defined.

How it works.

The layout design is processed into two passages: "measurement" and "positioning".

Each of the passages runs through the "view tree" from top to bottom.
During the "measurement" passage each view in the "tree" informs its size specifications and calculates its size. At the end all views have saved their dimensions.

The "measurement" passage uses the class Viewgroup.Layoutparams to communicate dimensions. It is used by "children" objects to tell "parents" how they want to be measured and positioned. The Viewgroup.Layoutparams base class only describes how large the view wants to be for both width and height.

For each dimension, it specifies one of the following values::

  • An exact number.
  • MATCH_PARENT, which means the view wants to be as big as the parent (less padding)
  • WRAP_CONTENT, means the view wants to be large enough to include its content (more padding).

During the "positioning" passage each "parent" view is responsible for positioning each "child" using the calculated sizes in the "measuring passage".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.