2013-08-16 57 views
0

我试图创建一个可点击的图像按钮W /文本,适合一个Horizo​​ntalScrollView内的列表。图像/内容将以编程方式设置。做到这一点的最佳方式似乎是一个LinearLayout,然后包含一系列RelativeLayouts,其中包含显示相关内容的视图。但是,我无法在每个RelativeLayout之间获取空间。尽管我已经在xml中设置了边距并以编程方式设置,但它们似乎被忽略,并且RelativeLayout对象被挤压在一起。如何在LinearLayout中的连续RelativeLayouts之间获得空间?

一些代码:

<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之间的余量或缓冲区?

+0

这并不能真正“解决”你的问题,但你可以改用'padding'而不是'margin'。 – Shadesblade

+0

我几乎可以肯定,这个问题源于没有将父容器传递给你的'inflate()'调用(这会抛开边距,因为它不知道要使用什么类型的LayoutParams,所以它回退到ViewGroup。的LayoutParams)。不要传递null,而是调用'inflate(R.layout.details_image_button,parent,false);'(或者如果您希望它立即连接,则为true)。 – kcoppock

回答

3

如果您RelativeLayoutLinearLayout里面,你需要使用LayoutParamsLinearLayout.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来自父母,而不是孩子。

相关问题