Already this xml went over and over, but the result is 0. Tell me where is the joint? That is how it should be:

It should be

Here a long text replaces the button on the screen:

The button goes behind the screen

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.user.myapplication.MainActivity" tools:showIn="@layout/app_bar_main"> <ProgressBar android:id="@+id/main_progress" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:visibility="gone" /> <ScrollView android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:layout_alignParentTop="true" android:layout_alignParentStart="true"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@drawable/separator" android:orientation="vertical" android:showDividers="middle" > <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:columnCount="3" android:orientation="horizontal" android:layout_weight="0.4"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:padding="12dp" android:text="@string/firstheader" android:textColor="#00b0e4" android:textStyle="bold" android:textAllCaps="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" style="?android:attr/buttonBarButtonStyle" android:textAllCaps="false" android:textColor="#666666" android:layout_rowSpan="2" android:minLines="2" android:id="@+id/report_button" /> </GridLayout> <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left" android:columnCount="2" android:orientation="horizontal"> <TextView android:padding="12dp" android:text="@string/secondheader" android:layout_columnSpan="2" android:layout_gravity="fill|left" android:textColor="@color/colorHeader" android:textStyle="bold" android:textAllCaps="true" android:layout_marginBottom="-10dp" /> <Button style="?android:attr/buttonBarButtonStyle" android:layout_gravity="left" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button21" android:textColor="@color/colorText"/> <Button android:id="@+id/button22" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="right" android:textAllCaps="false" /> <Button style="?android:attr/buttonBarButtonStyle" android:layout_gravity="left" android:id="@+id/button23" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorText" /> <Button android:textAllCaps="false" android:layout_gravity="right" android:id="@+id/button24" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button style="?android:attr/buttonBarButtonStyle" android:layout_gravity="left" android:id="@+id/button25" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorText" /> <Button android:textAllCaps="false" android:layout_gravity="right" android:id="@+id/button26" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <Button android:layout_gravity="fill" android:layout_columnSpan="2" style="?android:attr/buttonBarButtonStyle" android:textAllCaps="false" android:text="@string/othertest" android:textColor="@color/colorHeader" android:id="@+id/otherschedulebutton" android:layout_marginTop="-15dp" /> </GridLayout> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:columnCount="2" android:orientation="horizontal" android:layout_weight="0.3" > <TextView android:padding="12dp" android:text="@string/thirdheader" android:layout_columnSpan="2" android:layout_gravity="fill|left" android:textColor="@color/colorHeader" android:textStyle="bold" android:textAllCaps="true" android:layout_marginBottom="-10dp" /> <TextView android:id="@+id/btn31" style="?android:attr/buttonBarButtonStyle" android:textAllCaps="false" android:inputType="textMultiLine" android:textColor="@color/colorText" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn32" android:layout_gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/btn33" style="?android:attr/buttonBarButtonStyle" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textMultiLine" android:textColor="@color/colorText"/> <Button android:id="@+id/btn34" android:layout_gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/btn35" style="?android:attr/buttonBarButtonStyle" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textMultiLine" android:textColor="@color/colorText"/> <Button android:id="@+id/btn36" android:layout_gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_gravity="fill" android:layout_columnSpan="2" style="?android:attr/buttonBarButtonStyle" android:textAllCaps="false" android:text="@string/othertest" android:textColor="@color/colorHeader" android:id="@+id/other_status_button" android:layout_marginTop="-15dp" android:layout_marginBottom="-5dp" /> </GridLayout> </LinearLayout> </ScrollView> </LinearLayout> 

    2 answers 2

    You use weight, but you did not reset the corresponding parameter of the view. In your case, you need to zero the width instead of wrap_content .

    There is also absolutely no point in using GridLayout in your case. You can limit LinearLayout to LinearLayout Moreover, the GridLayout should not be displayed normally in ScrollView


    This markup will allow the right button to take all the space it needs, and the left text will take all the remaining

     <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="12dp" android:text="@string/firstheader" android:textColor="#00b0e4" android:textStyle="bold" android:textAllCaps="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/buttonBarButtonStyle" android:textAllCaps="false" android:textColor="#666666" android:id="@+id/report_button" /> </LinearLayout> 
    • With weight, I already started playing it later. I also used LinearLayout, but in this case only the buttons that were near the text flew away. How to deal with the fact that in LinearLayout with a horizontal orientation the element does not push the text abroad? - user200303
    • @ user200303, cited an example implementation in the answer. Details depend on the specific task. - Yuriy SPb ♦

    If you are using layout_weight, then you must make layout_width equal to 0 for horizontal layout or layout_height = 0dp for vertical. For your case should be like this:

      <TextView android:layout_width="**0dp**" android:layout_height="wrap_content" android:layout_weight="1" android:padding="12dp" android:text="@string/firstheader" android:textColor="#00b0e4" android:textStyle="bold" android:textAllCaps="true" /> 

    • It was already an attempt to fix something. It does not affect the result - user200303