android - LinearLayout 中元素间距的最佳实践

标签 android android-layout android-linearlayout

我创建了一个包含三个相同元素的简单 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 空间。

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

enter image description here 或者: enter image description here

或者其他的?

最佳答案

试试这个。

<?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" />

关于android - LinearLayout 中元素间距的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46670466/

相关文章:

android - 'netstat' 命令在 Android 上是否普遍可用?

android - Kotlin onClick事件和架构最佳实践

android - 是否可以在 Kotlin Anko 中重用布局

Android 在元素下方包含布局

android - 动态 TextViews 换行到线性布局中的下一行/行

android - 将多个嵌套的 LineairLayout 转换为单个 RelativeLayout

android - 在 Android 中使用 React Native 将简单数据发送到其他应用程序

android - 当前选择的 Java 调试器不支持类型为 'Kotlin Line Breakpoints' 的断点

android - 自定义通知最大高度?

android - 在将 Activity xml 的布局从 LinearLayout 更改为 RelativeLayout 后,获取 ClassCast 异常