【问题标题】:Space between LinearLayout children in HorizontalScrollViewHorizo​​ntalScrollView 中 LinearLayout 子项之间的空间
【发布时间】:2015-02-28 03:50:47
【问题描述】:

我无法在作为 Horizo​​ntalScrollView 的子项的 LinearLayout 中分离子项。我在 LinearLayout 元素(padding_right、layout_margin、divider)上尝试了多种方法,但都没有工作。下图说明了我想要实现的目标(顶部是预期结果,底部 1 是我的布局的渲染结果)

这是我使用的两种布局:

布局/activity_main.xml

<HorizontalScrollView
    android:id="@id/horizontal_scroll_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbarStyle="outsideInset"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/content_scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:showDividers="middle" >

        <include
            android:id="@+id/id"
            layout="@layout/gallery_item_layout" />

        <include
            android:id="@+id/id"
            layout="@layout/gallery_item_layout" />

        <include
            android:id="@+id/id"
            layout="@layout/gallery_item_layout" />

        <include
            android:id="@+id/id"
            layout="@layout/gallery_item_layout" />

        <include
            android:id="@+id/id"
            layout="@layout/gallery_item_layout" />

        <include
            android:id="@+id/id"
            layout="@layout/gallery_item_layout" />
    </LinearLayout>
</HorizontalScrollView>

布局/gallery_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/galleryItemLayout"
    android:layout_width="139.0dip"
    android:layout_height="130.0dip"
    android:background="@drawable/gallery_item_selector"
    android:focusable="true"
    android:padding="2dp" >

    <ImageView
        android:id="@id/galleryItemView"
        android:layout_width="139.0dip"
        android:layout_height="78.0dip"
        android:background="#00000000"
        android:padding="0.0dip" />

    <TextView
        android:id="@id/articleTitleId"
        android:layout_width="139.0dip"
        android:layout_height="50.0dip"
        android:layout_below="@id/galleryItemView"
        android:background="#ff0a0a0a"
        android:ellipsize="end"
        android:maxLines="2"
        android:paddingBottom="5.0dip"
        android:paddingLeft="5.0dip"
        android:paddingTop="2.0dip"
        android:textColor="#ffffffff"
        android:textSize="13.0sp" />

</RelativeLayout>

这是一些尝试的渲染结果:

使用 layout_marginLeft:

<LinearLayout
android:id="@+id/content_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_marginLeft="20dp" >

使用 paddingLeft:

<LinearLayout
android:id="@+id/content_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="20dp" >

【问题讨论】:

  • 设置自定义布局边距。
  • @SurenderKumar 我做到了。但结果适用于父母。我将在此处发布一些屏幕截图以供尝试。

标签: android layout horizontalscrollview


【解决方案1】:

您可以尝试将边距应用于包含布局

  <include 
android:id="@+id/id"
 layout="@layout/gallery_item_layout"
    android:layout_margin="2dp" /> 

【讨论】:

  • 这可行,但是当我打开“显示布局边界”选项时,结果显示为“粉红色区域”。 bbc 应用程序中的布局没有这个并且显示为一个清晰/空白的区域。有什么想法吗?
  • 能不能把截图分享给我,方便我理解
  • 它类似于上面的倒数第二个屏幕截图,只是粉红色区域位于框之间。
  • 尝试从中删除marginleft或尝试应用透明色背景
  • 也许我应该改写我的问题。您的解决方案有效。只是看起来 bbc 应用程序没有使用 layoutmargin 来分隔元素。你能想到的任何替代方案?因为使用边距它会在“开发者选项”中的“显示布局边界”下显示为粉红色区域
【解决方案2】:

您应该将边距添加到您的gallery_item_layout.xml

所以在您的 gallery_item_layout.xml 中为您的根 RelativeLayout 插入边距

android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"

或者,您可以为activity_main.xml 中的每个包含元素添加边距,但如果采用这种方法,您还必须为每个包含元素指定layout_widthlayout_height 参数。否则边距将被忽略。

【讨论】:

  • 这可行,但是当我打开“显示布局边界”选项时,结果显示为“粉红色区域”。 bbc 应用程序中的布局没有这个并且显示为一个清晰/空白的区域。有什么想法吗?
  • 我不知道他们是如何在 bbc 应用程序中做到这一点的。似乎他们以某种方式使用填充而不是边距来做到这一点。但我对此一无所知。如果我发现任何东西会让你。但只要最终结果对你来说没问题,我认为这不是你如何实现它的问题
【解决方案3】:

在您的布局/gallery_item_layout.xml 中

galleryItemLayout 的宽度应至少比 galleryItemViewarticleTitleId 的宽度多 2dp(如果您提供 2dp 的填充)

【讨论】:

  • 以及如何解决 main_activity 中的间隔问题?