【问题标题】:Android Linear Layout gives me headacheAndroid 线性布局让我头疼
【发布时间】:2011-12-05 16:43:37
【问题描述】:

我有以下 xml 文件:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:background="@android:color/transparent"
    android:layout_marginTop="0px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal">
      <Button android:id="@+id/info"   android:text="Info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom"></Button>
      <Button android:id="@+id/town"   android:text="Town" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom"></Button>
      <Button android:id="@+id/unit"   android:text="Unit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom"></Button>
      <Button android:layout_height="wrap_content" android:text="EndTurn" android:id="@+id/endturn" android:layout_width="wrap_content" android:layout_gravity="bottom"></Button>
  </LinearLayout>

提供以下结果:http://i42.tinypic.com/otdkb4.png

现在我有一些关于此的问题:

  1. 顶部和底部的内边距,如何去掉? 我尝试了RelativeLayout,相互之间的多个布局,填充,边距,改变高度似乎没有任何影响。

  2. 有没有办法让布局透明? android:background 似乎是错误的。

  3. 在第三个和第五个按钮之间有更多的空间(第四个按钮应该在哪里)。我在程序中捕获它并将其设置为不可见。

    unitButton.setVisibility(INVISIBLE); unitButton.setWidth(0);

现在两个按钮之间的空间是正常范围的两倍多(1 到 2 之间)对此有什么想法吗? - 虽然这是一个小问题

提前致谢。

【问题讨论】:

  • 请提出一个真正的问题。

标签: android android-layout android-linearlayout android-xml


【解决方案1】:

1: 布局是否在对话框中呈现?如果是这样,那会让你有些头疼。要获得更多控制权,您应该创建自己的自定义对话框扩展(因为某些对话框布局值是硬编码的),或者以其他方式显示您的布局(顶部的新活动,或者可能使用框架布局)?

2:要使布局透明,不要给它背景属性。 (不过,如果您真的在使用对话框,则对话框不是透明的,而是您所看到的。您也可以通过将背景设置为“#00000000”(这就是您所做的)来将其设置为透明。

3:仍会测量可见性为“不可见”的视图,这意味着它的宽度/高度以及边距和填充都显示为布局中的空白空间。将可见性设置为“消失”不会测量它,您也不需要 setWidth(0)。 (您仍然可以稍后通过将其设置回“可见”来显示它)

编辑:删除未使用的“weightSum”属性也可能是个好主意,因为视图现在期望其子项的总权重不是 0。

【讨论】:

  • 实际上我使用一个对话框,并用它做很多事情 - 例如。不锁定背景。我试图删除 .setContentView 和 .show 旁边的所有内容,问题仍然存在。我将研究对话 - 从来没有被认为是那里的问题。所以谢谢输入