1

这是我在创建具有可自定义过度滚动的 ListView 方面所做的更多工作。

我想创建一个扩展 RelativeLayout 但添加子视图的自定义元素根本无法正常工作。代码看起来正确,但视图看起来有点疯狂。

    underscrollEdge = new ImageView(context);
    underscrollEdge.setImageResource(R.drawable.underscroll_edge);
    underscrollGlow = new ImageView(context);
    underscrollGlow.setImageResource(R.drawable.underscroll_glow);
    overscrollGlow = new ImageView(context);
    overscrollGlow.setImageResource(R.drawable.overscroll_glow);
    overscrollEdge = new ImageView(context);
    overscrollEdge.setImageResource(R.drawable.overscroll_edge);

    RelativeLayout.LayoutParams topLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    topLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    addView(underscrollEdge, topLayout);
    addView(underscrollGlow, topLayout);        

    RelativeLayout.LayoutParams bottomLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    bottomLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    addView(overscrollGlow, bottomLayout);      
    addView(overscrollEdge, bottomLayout);  

奇怪的是,这给出了这个(我已将相对布局设置为黑色以显示边缘和发光):

如您所见,顶部边缘漂浮在不知名的地方,底部发光已缩小到很小的尺寸......到底是什么?

4

1 回答 1

0

喜欢写下问题如何帮助我解决它:

    RelativeLayout.LayoutParams topLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    topLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    addView(underscrollGlow, topLayout);        
    topLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    topLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    addView(underscrollEdge, topLayout);    

    RelativeLayout.LayoutParams bottomLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    bottomLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    addView(overscrollGlow, bottomLayout);      
    bottomLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    bottomLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    addView(overscrollEdge, bottomLayout);  

对两个组件使用相同的 LayoutParams 会使事情变得很奇怪。看起来很繁重,但它确实可以解决问题。

于 2011-07-22T10:55:44.620 回答