开发者

Extending RelativeLayout

开发者 https://www.devze.com 2023-03-22 09:57 出处:网络
This is more work I\'m doing on creating a ListView with customisable overscroll. I want to create a Custom element which extends RelativeLayout but adding child view\'s isn\'t working at all well. T

This is more work I'm doing on creating a ListView with customisable overscroll.

I want to create a Custom element which extends RelativeLayout but adding child view's isn't working at all well. The code seems right but the view looks slightly insane.

    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.WR开发者_如何学CAP_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);  

This, strangely, gives this (I've set the relative layout to black to show the edges and glows):

Extending RelativeLayout

As you can see, the top edge is floating in the middle of nowhere and the bottom glow has shrunk to a teeny tiny size.... What the heck?


Love how writing down the problem helps me solve it:

    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);  

Using the same LayoutParams for two components make's things go weird. Seems onerous but it does fix things.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号