按钮宽度与父android不匹配



我有以下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>

按钮1用于切换栅格视图的可见性。但是,当网格视图不可见时,按钮不会填充屏幕。它们占据了屏幕的一半。当网格视图可见时,按钮会按我的意愿占据整个屏幕。我一直无法理解这种奇怪的行为。

尝试将Linear layout宽度更改为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" >

之所以会发生这种情况,是因为您将LinearLayout的宽度给定为wrap_content。您必须将LinearLayout的宽度定义为fill_path或match_parent。

您的按钮1在text_view上对齐。

因此,尝试在父布局的顶部添加高度为1dp的简单视图,并使其与宽度匹配。

当这样的事情发生时,我就是这样做的。

最新更新