Answer the question
In order to leave comments, you need to log in
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>
<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>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question