【问题标题】:Button width does not match parent android按钮宽度与父 android 不匹配
【发布时间】:2013-02-05 08:20:53
【问题描述】:

我有以下 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 用于切换网格视图的可见性。但是,当网格视图不可见时,按钮不会填满屏幕。它们占据了屏幕的一半。但是,当网格视图可见时,按钮会按照我的意愿占据整个屏幕。我一直无法理解这种奇怪的行为。

【问题讨论】:

    标签: android android-layout button android-linearlayout android-gridview


    【解决方案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" >
    

    【讨论】:

    • 抱歉,我已将宽度和高度都更改为 match_parent。没什么区别:(
    • 只尝试 width=match_parent 和 height=wrap_content
    【解决方案2】:

    这是因为您将 LinearLayout 的宽度指定为 wrap_content。您必须将 LinearLayout 的宽度定义为 fill_parent 或 match_parent。

    【讨论】:

      【解决方案3】:

      您的 button1 与您的 text_view 对齐。

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

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

      【讨论】:

        猜你喜欢
        • 2018-10-05
        • 2018-12-18
        • 1970-01-01
        • 1970-01-01
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多