扩展RelativeLayout

发布于 2024-11-25 11:50:51 字数 1309 浏览 0 评论 0原文

这是我在创建具有可自定义过度滚动的 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);  

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹倦 2024-12-02 11:50:51

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

    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 会让事情变得很奇怪。看起来很繁重,但它确实可以解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文