【问题标题】:Invisible components still take up space不可见的组件仍然占用空间
【发布时间】:2013-09-13 13:47:58
【问题描述】:

所以,我有一个看起来像这样的活动:

http://i.imgur.com/UzexgEA.jpg

如你所见,它有一个按钮、一个列表和一个按钮。

如果满足某些条件,我想隐藏两个按钮只显示列表,但正如您在下一张图片中看到的那样,按钮仍然占用空间:

http://i.imgur.com/OyLIfSk.jpg

所以我的问题是,我可以做些什么来扩大列表以腾出空间?

这是我的按钮和列表布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/findSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="registrarAsistencia"
        android:text="Registrar Asistencia" />

    <ListView
        android:id="@+id/listaAlumnos"
        android:layout_width="fill_parent"
        android:layout_height="376dp"
        android:layout_weight="2.29" />

    <Button
        android:id="@+id/btnChecarBoxes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="seleccionarTodosNinguno"
        android:text="Seleccionar / Deseleccionar todo" />
</LinearLayout>

这是列表内容的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:orientation="vertical" >

      <TextView
          android:id="@+id/rowTextView"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:padding="10dp"
          android:textSize="16sp" 
          android:textStyle="bold"/>

      <TextView
          android:id="@+id/rowTextView2"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:padding="10dp"
          android:textSize="16sp" 
          android:textStyle="italic"/>

  </LinearLayout>

  <CheckBox
      android:id="@+id/CheckBox01"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_alignParentRight="true"
      android:layout_centerVertical="true"
      android:focusable="false"
      android:padding="10dp" />
</RelativeLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    不要制作按钮View.INVISIBLE。制作按钮View.GONE。共有三种可见性状态:

    • 可见(正常)
    • 不可见(像素未绘制,但仍占用空间)
    • 消失(不包括在渲染中)

    【讨论】:

    • 这就像一个魅力。非常感谢您的快速答复!
    【解决方案2】:

    在java代码中

    yourView.setVisibility(View.GONE);
    

    在 XML 代码中

    android:visibility="gone"
    

    他们是 3 个州

    1. 可见:正常
    2. 不可见:占用空间且不可见
    3. 消失了:没有空间也没有可见性

    【讨论】:

      【解决方案3】:

      使用

      android:layout_height="0dp"
      android:layout_weight="1"
      

      除了@CommonsWare 的回答

      您应该使用适合所有屏幕尺寸的布局。如果你给你布局一个固定的高度,比如android:layout_height="376dp",它只会在你正在测试的设备(或模拟器)上看起来不错。您需要做的是确保您的列表视图占用所有(且仅)您的按钮留下的空间。

      查看此article 和此answer 以更好地了解布局权重。

      【讨论】:

      • 我真的是 Android 的初学者,所以,请解释一下为什么使用 0dp 和 1 而不是 match_parent 和/或 wrap_content?。提前致谢。
      • @Moko 我刚刚用更多信息更新了我的答案。我希望它有所帮助。
      • 知道了,看看那些。感谢您的回答!
      【解决方案4】:

      用于隐藏您的小部件

         yourEditText.setVisibility(View.GONE)
      

      为了您的小部件的可见性

         yourEditText.setVisibility(View.VISIBLE)
      

      【讨论】:

        猜你喜欢
        • 2018-11-26
        • 1970-01-01
        • 2019-08-02
        • 1970-01-01
        • 1970-01-01
        • 2018-02-26
        • 1970-01-01
        • 1970-01-01
        • 2013-08-27
        相关资源
        最近更新 更多