2013-07-07 66 views
23

當擴展一個Android的ViewGroup類,什麼是onLayout()覆蓋的目的是什麼?我正在Android中進行自定義控件,但由於某種原因,內容(子對象View)未顯示。我的方法是擴展ViewGroup類,通過ViewGroup的addView()方法添加子視圖。然後,在我的主要活動,我有以下代碼:Android ViewGroup:我應該怎麼做onLayout()覆蓋?

ChannelController myCC = new ChannelController(this); 
setContentView(myCC); 

ChannelController是我的自定義類的擴展ViewGroup中的名稱。我必須做錯事,因爲屏幕上沒有任何東西顯示。

我知道我必須覆蓋並實現onLayout()方法,而是用什麼呢?我知道在dev.android網站上有一整個頁面專用於此,但它對我沒有多大幫助,主要是因爲我是一個新手。任何洞察力將不勝感激。

僅供參考,我的ViewGroup擴展看起來象下面這樣:

public class ChannelController extends ViewGroup { 

    final String TAG = "JAL"; 

    public ChannelController(Context c) 
    { 
     super(c); 
     init(c); 
    } 

    public ChannelController(Context c, AttributeSet attibset) 
    { 
     super(c); 
     init(c); 
    } 

    public ChannelController(Context c, AttributeSet attribset, int defStyle) 
    { 
     super(c); 
     init(c); 
    } 

    public void init(Context c) 
    { 
     //RelativeLayout wrap = new RelativeLayout(c); 
     RelativeLayout.LayoutParams wrapLP = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

     RelativeLayout r1 = new RelativeLayout(c); 
     RelativeLayout.LayoutParams r1LP = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

     RelativeLayout r2 = new RelativeLayout(c); 
     RelativeLayout.LayoutParams r2LP = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

     TextView t = new TextView(c); 
     RelativeLayout.LayoutParams tlp = new RelativeLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     Button m = new Button(c); 
     RelativeLayout.LayoutParams mlp = new RelativeLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     Button s = new Button(c); 
     RelativeLayout.LayoutParams slp = new RelativeLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     SeekBar f = new SeekBar(c); 
     RelativeLayout.LayoutParams flp = new RelativeLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     t.setId(1); 
     m.setId(2); 
     s.setId(3); 
     f.setId(4); 
     r1.setId(5); 
     r2.setId(6); 

     t.setText("CHANNELNAME"); 
     t.setTextColor(Color.BLACK); 
     tlp.setMargins(30, 0, 0, 0); 
     tlp.addRule(RelativeLayout.LEFT_OF, m.getId()); 
     tlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     tlp.addRule(RelativeLayout.CENTER_VERTICAL); 
     tlp.addRule(RelativeLayout.CENTER_HORIZONTAL); 

     m.setText("M"); 
     m.setBackgroundColor(Color.rgb(237, 155, 31)); 
     m.setTextColor(Color.WHITE); 
     mlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     mlp.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     m.setTextSize(10); 

     flp.addRule(RelativeLayout.LEFT_OF, s.getId()); 
     flp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     flp.addRule(RelativeLayout.CENTER_VERTICAL); 

     s.setText("S"); 
     s.setTextSize(10); 
     s.setBackgroundColor(Color.rgb(192, 48, 46)); 
     s.setTextColor(Color.WHITE); 
     slp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     slp.addRule(RelativeLayout.ALIGN_PARENT_TOP); 

     r1.addView(t, tlp); 
     r1.addView(m, mlp); 
     r2.addView(f, flp); 
     r2.addView(s, slp); 

     r1.setBackgroundColor(Color.rgb(233, 242, 251)); 
     r2.setBackgroundColor(Color.rgb(233, 242, 251)); 

     r1LP.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     r2LP.addRule(RelativeLayout.BELOW, r1.getId()); 

     this.addView(r1, r1LP); 
     this.addView(r2, r2LP); 

     this.setLayoutParams(wrapLP); 

     //this.addView(wrap); 

     Log.i(TAG, "ChannelController constructor was called"); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // TODO Auto-generated method stub 
     //super.onLayout(changed, l, t, r, b); 

    } 

} 

我需要什麼,在onLayout方法重寫呢?

+0

相關:http://stackoverflow.com/questions/6843138/viewgroup-onlayout-problem –

回答

36

onLayout你需要調用layout方法對此ViewGroup的每個孩子,並提供他們所需的位置(相對於父母)。您可以檢查FrameLayoutViewGroup的最簡單子類之一)的源代碼,以瞭解它的工作原理。

儘管如此,如果你不需要任何「特殊」的布點,你還有其他選擇:

  • 延長ViewGroup代替(FrameLayout例如)某些另一個子
  • 使用LayoutInflater如果你只需要你的控制看起來完全按照XML(我想這,也正是你的情況)
+0

嗨梅德採夫..我我正面臨着山姆即使在使用您的解決方案後仍無法解決問題。我已經在這個環節上規定我的問題:http://stackoverflow.com/questions/19311549/after-inflating-and-assigning-a-layout-to-view-the-elements-inside-layout-not-s/19311818 ?noredirect = 1#19311818。請幫助 – Sushil

+0

如果您要擴展'ViewGroup',請在哪裏擴充XML佈局? –

+1

@anoniim內自定義'的構造ViewGroup' –