【问题标题】:Best practices for spacing between elements in LinearLayoutLinearLayout 中元素间距的最佳实践
【发布时间】:2017-10-10 15:24:22
【问题描述】:

我创建了一个简单的LinearLayout,其中包含三个相同的元素:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Hello"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Hello"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Hello"/>
</LinearLayout>

现在我要在每对元素之间引入8dp 空格。

以下哪种解决方案被认为更清洁?

或者:

或者其他?

【问题讨论】:

  • 如何使用视图并将其放在视图之间?
  • @MohammadZarei 你的意思是这里描述的解决方案吗:stackoverflow.com/a/18538656/1494902
  • 我建议使用第二个:将来您可能希望在某些自定义类中更改TextView,并且可以将其嵌入到类中。或者您可能想要为该TextView 创建一个style 资源并在style 中定义边距。

标签: android android-layout android-linearlayout


【解决方案1】:

试试这个。

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

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Hello" />

<android.support.v4.widget.Space
    android:layout_width="8dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Hello" />

<android.support.v4.widget.Space
    android:layout_width="8dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Hello" />
</LinearLayout>

为您的代码添加空间。

<android.support.v4.widget.Space
    android:layout_width="8dp"
    android:layout_height="wrap_content" />

【讨论】:

    【解决方案2】:

    无论是给定的解决方案,您还可以为LinearLayout 使用可绘制分隔线

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:showDividers="middle"
        android:divider="@drawable/divider8p"
        android:orientation="vertical">
    
        <!-- Your items here -->
    
    </LinearLayout>
    

    对于分隔符声明:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="#00FFFFFF"/>
        <size android:width="8dp" android:height="8dp"/>
    </shape>
    

    【讨论】:

      【解决方案3】:

      我喜欢已经发布的Space 解决方案,但我想在原始问题的精神中添加另一个答案。

      在 OP 呈现的情况下,如果我要使用边距来完成此操作,我将在除第一个元素之外的每个元素上使用开始/左边距。我会这样做是因为我预测未来布局可能会发生变化。在我看来,最有可能发生的事情是在LinearLayout 的末尾添加新元素或从LinearLayout 的末尾删除元素。在这些情况下,如果我在每个项目上使用开始/左边距,我可以删除单个视图而无需触及其他视图。

      【讨论】:

        猜你喜欢
        • 2018-03-31
        • 2015-07-08
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多