2011-07-22 20 views
1

這是我正在做更多的工作,創建ListView與自定義overscroll。擴展RelativeLayout

我想創建一個自定義元素,它擴展了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); 

這奇怪的是,給出了這樣的(我已經設置相對佈局,以黑色顯示的邊緣和光暈):

正如你可以看到,頂部邊緣浮動在無處不在的地方,底部的光芒已經縮小到一個小小的尺寸......這到底是什麼?

回答

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做的事情很奇怪。似乎繁重,但它確實解決了一些問題。