问题
I have the following xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dip"
android:text="Fragment1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"/>
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="match_parent"overla
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"/>
</LinearLayout>
The button1 is used to toggle the visibility of the grid view. But, when the grid view is not visible, the buttons don't fill the screen. They occupy half the screen. When the grid view is visible though, the buttons occupy all of the screen as I want them to. I haven't been able to understand this strange behaviour.
回答1:
Try to change the width of the Linear layout
to match_parent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
回答2:
This is happening because you have given width of LinearLayout as wrap_content. You have to define width of LinearLayout as fill_parent or match_parent.
回答3:
Your button1 aligned on your text_view.
So try to add simple View with height 1dp in top of your parent Layout and make it match_parent width.
That's how I do, when something like this happens.
来源:https://stackoverflow.com/questions/14702941/button-width-does-not-match-parent-android