0

我正在尝试创建一个可点击的图像按钮列表,其中包含适合水平滚动视图的文本。图像/内容将以编程方式设置。做到这一点的最好方法似乎是 LinearLayout,然后包含一系列 RelativeLayouts,其中包含用于显示相关内容的视图。但是,我无法在每个 RelativeLayout 之间获得空间。即使我已经在 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"
        />

//Code below is looped through several times
        RelativeLayout imageButtonLayout = (RelativeLayout) inflater.inflate(R.layout.details_image_button, null);
        RelativeLayout.LayoutParams imageButtonLayoutParams = new RelativeLayout.LayoutParams(100, 100);
        imageButtonLayoutParams.setMargins(10, 10, 10, 10);
        imageButtonLayout.setLayoutParams(imageButtonLayoutParams);

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

4

1 回答 1

3

如果您RelativeLayout在 a 内LinearLayoutLayoutParams您需要使用的是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 来自父级,而不是子级。

于 2013-08-17T00:01:23.743 回答