TwoWayView android库如何设置Margin(图像之间的间距)? setItemMargin()不起作用

时间:2014-07-20 08:08:48

标签: java android android-layout

我正在使用TwoWayView库,但我无法在图像之间设置边距。我使用了 setItemMargin(1),但它不起作用。 当我传递大值(75,100)时,它会增加图像之间的空间,但是当我尝试将图像设置得彼此非常接近时,可以说2或5它不起作用,对于低于10默认边距的所有值,间距将相同,如图片如下。

动态添加视图的代码

   protected void showTwowayview() {

    // Get the welcome card layout and add this layout
    // below that.
    adapter = new HorizontalImagesBaseAdapter(MainActivity.this,
            ImageApplication.fromRecorderPaths);
    RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.welcomeCard);
    Log.d("in", "in");
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 300);
    // Id of the view below which twoway view will be displayed
    params.addRule(RelativeLayout.BELOW, R.id.mannual);
    // set the layout margin
    params.setMargins(LEFT, TOP, RIGHT, BOTTOM);
    myLayout.setLayoutParams(params);
    TwoWayView lparams = (TwoWayView) getLayoutInflater().inflate(
            R.layout.twowayview, myLayout, false);
    TwoWayView twowayview = (TwoWayView) lparams.findViewById(R.id.lvItems);
    myLayout.addView(twowayview, 0);
    //0 is the pisition off the twowayview in the layout
    TwoWayView listView = (TwoWayView) myLayout.getChildAt(0);

    //listView.setItemMargin(1);
    listView.setAdapter(adapter);
}

Twowayview View

<?xml version="1.0" encoding="utf-8"?>
<org.lucasr.twowayview.TwoWayView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/lvItems"
style="@style/TwoWayView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:background="@android:color/holo_green_light"
tools:context=".MainActivity" />

在下面的截图中,我得到的最小间距是什么。但我希望图像彼此非常接近。 ScreenShot

1 个答案:

答案 0 :(得分:1)

请注意,像素中的值不是DPI,因此您需要将值从DPI转换为Pixel,然后再将其传递给setItemMargin