【问题标题】:How to specify spacing between elements of LinearLayout only once?如何仅指定一次 LinearLayout 元素之间的间距?
【发布时间】:2012-09-09 09:56:42
【问题描述】:

我最近又遇到了一个问题,过去几年我已经遇到过好几次了。

LinearLayout 是一个非常方便的layout manager。但我完全想念的是在单个 XML 标记中的元素(如填充)之间添加一定空间的可能性。

我所说的一个标签的意思是,我可以在 LinearLayout 的声明中定义元素之间的间距(例如,在垂直 LinearLayout 中,此布局中两个元素之间的垂直间距)。

我知道我可以通过添加 XML 标记 android:layout_marginTop 或类似于 LinearLayout 中的每个元素的东西来做到这一点。

但我希望能够只在一个点上定义它,因为所有元素的间距都是相同的。

有没有人知道一个简单的方法来做到这一点(不实现自定义 LinearLayout 或类似的东西)?我更喜欢直接在 XML 中工作而无需编码的解决方案。

【问题讨论】:

标签: android android-layout android-widget android-linearlayout


【解决方案1】:

推荐的方式是对线性布局中的所有元素应用一个样式

android:style="@style/mystyle"

<style name="mystyle">
      <item name="android:layout_marginTop">10dp</item>
      ... other things that your elements have in common
</style>

【讨论】:

  • 谢谢@MrFox。这就是我一直在寻找的解决方案。
  • 我和 OP 有同样的问题。但是,我不确定这种方法如何完成我正在寻找的东西。例如,如果我的 LinearLayout 中有 10 个文本视图,并且我希望它们之间的间距为 10dp,我希望能够在一个地方指定 10dp 设置。如果我在您的示例中定义样式,我仍然必须将此样式应用于所有 10 个文本视图。我在这里错过了什么吗?
  • @AngieM 使用这个SO Answer 它建议使用 LinearLayout 的分隔符在 LL 子级之间应用均匀的间距。
  • 别自欺欺人——你只会在 LinearLayout 中间隔特定元素。例如,如果我需要动态隐藏/显示其中一些 - 间距将被破坏(例如,如果我让第一个元素消失,我可能最终显示 LinearLayout 的上边距 + 第二个元素分隔符间距(这是多余的)的间距) .非常不可靠的解决方案,IMO
  • 很公平,在 2012 年我发帖时是当时最好的解决方案 :)
【解决方案2】:

将自定义透明可绘制对象设置为布局的分隔符:

<LinearLayout
  android:showDividers="middle"
  android:divider="@drawable/divider">

drawables 文件夹(divider.xml)中的新可绘制资源:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android">
  <size
    android:width = "0dp"
    android:height = "16dp"/>
</shape>

【讨论】:

  • 伙计,这个答案比公认的要好得多
【解决方案3】:

@Chris-Tulip 的回答对我很有帮助——也有很好的实践。

对于那些可能会遇到 Eclipse 错误的人,这些错误是关于在 android 包中缺少“样式”的资源标识符,就像我所做的那样,您不需要添加 android 命名空间。

所以, android:style="xx" 出现错误,而 style="xx" 是正确的。很时髦,但是对于任何有这个错误的人来说,这可能会有所帮助。

【讨论】:

    【解决方案4】:

    您应该将android:layout_marginTopandroid:layout_marginLeft 添加到必须具有缩进的元素中。取决于您的LinearLayout 中的android:orientation

    【讨论】:

      【解决方案5】:

      您可以在单独的 xml 文件中定义您的单个项目“原型”,然后在代码中动态扩展该文件中的项目并将它们插入到您的线性布局中。

      然后,您将在实际项目上定义间距,而不是在父 LinearLayout 上定义间距(例如 android:layout_marginTop),并且当您膨胀它们时,该间距将应用于您的所有项目。

      编辑:

      container.xml:

      <LinearLayout
          android:id="@+id/parent"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <!-- Your items will be added here -->
      
      </LinearLayout>
      

      item.xml:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="4dp">
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="This is my child" />
      
      </LinearLayout>
      

      MyActivity.java:

      // Put this in a suitable place in your Java code, perhaps
      // in "onCreate" or "onResume" depending on where and how
      // you initialize your view. You can, of course inflate
      // any number of instances of the item and add them to
      // your parent LinearLayout.
      LayoutInflater inflater = LayoutInflater.from(context);
      View item = inflater.inflate(R.layout.item, null, false);
      
      LinearLayout container = findViewById(R.id.parent);
      container.addView(view);
      

      我没有努力测试代码,但它“应该”按原样工作:-)

      【讨论】:

      • 感谢 dbm。感谢您提供漂亮的代码示例。我更喜欢直接在 XML 中工作的解决方案,但如果没有更好的解决方案出现,我会选择你的作为答案。
      • 我明白了。不幸的是,我不知道纯 XML 解决方案。
      • 永远不要在 Android 中使用 5dp。始终使用 4、8、16、32、48 dp。 developer.android.com/design/style/metrics-grids.html
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      相关资源
      最近更新 更多