按钮宽度与父级android不匹配

Nam*_*tha 3 android button android-layout android-linearlayout android-gridview

我有以下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>
Run Code Online (Sandbox Code Playgroud)

button1用于切换网格视图的可见性.但是,当网格视图不可见时,按钮不会填满屏幕.他们占据了屏幕的一半.当网格视图可见时,按钮占据了我想要的所有屏幕.我无法理解这种奇怪的行为.

Ner*_*een 5

尝试改变宽度Linear layoutmatch_parent

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)