Android 如何在线性布局中获得连续相对位置之间的空间?

Android 如何在线性布局中获得连续相对位置之间的空间?,android,layout,android-relativelayout,Android,Layout,Android Relativelayout,我正在尝试创建一个可单击的图像按钮列表,其中包含适合水平滚动视图的文本。图像/内容将以编程方式设置。实现这一点的最佳方法似乎是线性布局,然后它包含一系列RelativeLayouts,这些RelativeLayouts包含用于显示相关内容的视图。然而,我很难在每个相对者之间获得空间。尽管我用xml和编程方式设置了边距,但它们似乎被忽略了,并且RelativeLayout对象被挤压在一起 一些代码: <RelativeLayout android:id="@+id/details_i

我正在尝试创建一个可单击的图像按钮列表,其中包含适合水平滚动视图的文本。图像/内容将以编程方式设置。实现这一点的最佳方法似乎是线性布局,然后它包含一系列RelativeLayouts,这些RelativeLayouts包含用于显示相关内容的视图。然而,我很难在每个相对者之间获得空间。尽管我用xml和编程方式设置了边距,但它们似乎被忽略了,并且RelativeLayout对象被挤压在一起

一些代码:

<RelativeLayout
    android:id="@+id/details_image_button"
    android:layout_width="75dp"
    android:layout_height="100dp"
    android:layout_marginLeft="10dp"
    android:background="#00ff78">

    <ImageView
        android:id="@+id/loadable_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />

    <TextView
        android:id="@+id/details_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#b083ef"
        android:text="PH - Info about title"
        android:layout_alignParentBottom="true"
        />

我得到的当前结果是纯绿色(RelativeLayout的背景色),而不是一组RelativeLayout之间有空格的预期结果。如何才能最好地在每个RelativeLayout之间获得边距或缓冲区?

如果您的
RelativeLayout
位于
LinearLayout
内部,则需要使用的
LayoutParams
将是
LinearLayout.LayoutParams

RelativeLayout imageButtonLayout = (RelativeLayout) 
                                   inflater.inflate(R.layout.details_image_button, null);
    LinearLayout.LayoutParams imageButtonLayoutParams = new 
                                   LinearLayout.LayoutParams(100, 100);
    imageButtonLayoutParams.setMargins(10, 10, 10, 10);
    imageButtonLayout.setLayoutParams(imageButtonLayoutParams);

LayoutParams来自父级,而不是子级。

如果您的
RelativeLayout
位于
线性布局
内,则需要使用的
LayoutParams
将是
线性布局。LayoutParams

RelativeLayout imageButtonLayout = (RelativeLayout) 
                                   inflater.inflate(R.layout.details_image_button, null);
    LinearLayout.LayoutParams imageButtonLayoutParams = new 
                                   LinearLayout.LayoutParams(100, 100);
    imageButtonLayoutParams.setMargins(10, 10, 10, 10);
    imageButtonLayout.setLayoutParams(imageButtonLayoutParams);

LayoutParams来自父级,而不是子级。

这并不能真正“解决”您的问题,但您可以使用
填充
而不是
边距
。我几乎可以肯定,问题源于没有将父容器传递到
充气()调用中(由于它不知道要使用哪种类型的LayoutParams,因此会丢弃边距,因此它会返回到ViewGroup.LayoutParams)。与其传递null,不如调用
inflate(R.layout.details\u image_按钮,parent,false);
(如果要立即附加它,则为true)。这并不能真正“解决”您的问题,但您可以使用
填充
而不是
边距
。我几乎可以肯定,问题源于没有将父容器传递到
充气()
调用(调用将丢弃边距,因为它不知道要使用哪种类型的LayoutParams,所以它会返回到ViewGroup.LayoutParams)。不要传递null,而是调用
充气(R.layout.details\u image\u按钮,父项,false);
(如果要立即附加,则调用true)。