按钮宽度与父容器不匹配 Android。

3

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

button1用于切换网格视图的可见性。但是,当网格视图不可见时,按钮不会填充屏幕。它们只占据了屏幕的一半。然而,当网格视图可见时,按钮占据了整个屏幕,就像我想要的那样。我无法理解这种奇怪的行为。

3个回答

5
尝试将线性布局的宽度更改为match_parent。请注意保留HTML标记。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

哦,抱歉,我已经将宽度和高度都改为match_parent了。但是这并没有什么区别 :( - Namratha
请尝试仅使用width=match_parent和height=wrap_content。 - Nermeen

0
这是因为您将LinearLayout的宽度设置为wrap_content。您需要将LinearLayout的宽度定义为fill_parent或match_parent。

0

你的button1与text_view对齐。

尝试在父布局顶部添加一个高度为1dp的简单视图,并使其宽度match_parent。

当出现这种情况时,这就是我所做的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接