Horizo​​ntalScrollView中LinearLayout子项之间的空格

时间:2015-02-28 03:50:48

标签: android layout horizontalscrollview

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

Top is Expected Result, Bottom is Current Facing Problem

以下是我正在使用的2种布局:

布局/ 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" >

enter image description here

使用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" >

enter image description here

3 个答案:

答案 0 :(得分:2)

您可以尝试将保证金应用于包含布局

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

答案 1 :(得分:2)

您应该将余量添加到gallery_item_layout.xml

所以在gallery_item_layout.xml插入根RelativeLayout

的边距
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"

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

答案 2 :(得分:0)

在layout / gallery_item_layout.xml中

galleryItemLayout的宽度应该比galleryItemViewarticleTitleId的宽度至少2dp,如果你给出2dp的填充