Horizo​​ntalScrollView在视图之间添加空格

时间:2012-08-08 22:51:21

标签: android horizontalscrollview

我有Horizo​​ntalScrollView,问题是我添加视图后,这些视图之间有空格。这是一张照片:
enter image description here

以下是该类的代码:

public class HomeFeatureLayout extends HorizontalScrollView {
    public HomeFeatureLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public HomeFeatureLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeFeatureLayout(Context context) {
        super(context);
    }

    public void setFeatureItems() {
        LinearLayout internalWrapper = new LinearLayout(getContext());
        internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
        addView(internalWrapper);
        // this.mItems = items;
        for (int i = 0; i < 10; i++) {
            ImageView im = new ImageView(getContext());
            im.setImageResource(R.drawable.aaaa);
            im.setScaleType(ScaleType.CENTER_INSIDE);
            im.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));


            internalWrapper.addView(im);
        }
    }
}

和MainActivity:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HomeFeatureLayout home = new HomeFeatureLayout(this);
        home.setFeatureItems();
        home.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        setContentView(home);

    }

为什么每2张图片之间有空格?

1 个答案:

答案 0 :(得分:0)

我必须创建自己的视图并扩展ImageView,然后在THIS问题的帮助下覆盖OnMeasure方法:

public class myImageView extends ImageView {

    public myImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        MeasureSpec.getSize(widthMeasureSpec);
        MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));

        setLayoutParams(new LinearLayout.LayoutParams(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)));

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int screenWidth = display.getWidth();

        if (specMode == MeasureSpec.EXACTLY)
            // We were told how big to be
            result = specSize;
        else {
            // Measure the view
            result = screenWidth;
            if (specMode == MeasureSpec.AT_MOST)
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
        }

        return result;
    }

    public int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int screenHeight = display.getHeight();

        if (specMode == MeasureSpec.EXACTLY)
            // We were told how big to be
            result = specSize;
        else {
            // Measure the view
            result = screenHeight;
            if (specMode == MeasureSpec.AT_MOST)
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
        }

        return result;
    }

}