B
B
blantcat2016-07-28 23:22:03
Android
blantcat, 2016-07-28 23:22:03

Why does a button in a RelativeLayout stretch to full width?

Hello!
Just started learning Android and ran into a question. Why exactly in RelativeLayout does the button stretch to the full width, because the width of the layout is set to wrap_content?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button" />

</RelativeLayout>

94eec8c929.jpg
In LinearLayout , everything is correct:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2" />
</LinearLayout>

7804cb58b5.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-07-29
@blantcat

Why do you think the second option is correct? Thinking logically, the behavior is undefined - you're telling the container to "be as wide as the content". And the content - "fill the container". Make the container match_parent and everything will be ok.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question