2013-10-31 182 views
1

我想創建一個customview,它將允許我添加圖像和文本。我這樣做的原因是因爲在我創建的應用程序中,我一遍又一遍地重複相同的代碼以實現此目的,我想嘗試將它包裝到自己的自定義視圖中,以便我可以使用setter和position相對於彼此的視圖,將所有這些代碼放在一個簡單的使用類中。CustomView延伸RelativeLayout

我可以添加文字視圖和圖像到視圖,但是當我嘗試將它們相對於另一個放置時,我總是得到一個NPE崩潰。

這是我到目前爲止的代碼,它只是添加一個TextView,然後嘗試將TextView居中在我的自定義視圖中。

public class MultiView extends RelativeLayout { 
    Context cx; 
    int images = 0; 

    public MultiView(Context context) { 

     super(context); 
     cx = context; 
    } 

    public void addText(String textParam) { 

     TextView tv = new TextView(cx); 

     tv.setText(textParam); 

     tv.setTextColor(Color.WHITE); 

     RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv 
       .getLayoutParams(); 

     layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 

//  tv.setLayoutParams(layoutParams); // the app crashes when I add this line 

     this.addView(tv); 
    } 

} 

回答

2

滿級:

public class MultiView extends RelativeLayout { 
    Context cx; 
    int images = 0; 

    public MultiView(Context context) { 

     super(context); 
     cx = context; 
    } 

    public void addText(String textParam) { 

     RelativeLayout.LayoutParams layoutParams = 
     new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT,  
      RelativeLayout.LayoutParams.WRAP_CONTENT); 


     TextView tv = new TextView(cx); 
     tv.setText(textParam); 
     tv.setTextColor(Color.WHITE); 
     layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 
     tv.setLayoutParams(layoutParams); 
     this.addView(tv); 
    } 

} 
+0

非常感謝你的回答這個工作出色!我會把這個標記爲答案,因爲你給了我非常容易閱讀的完整課程! – brux

+0

我知道這是一個古老的答案,但是,你是否有任何想法,爲什麼'RelativeLayout.CENTER_IN_PARENT'的規則,如果這個'MultiView'被添加到'LinearLayout'中作爲父項? 因此視圖層次結構變爲LL> MultiView。 – nightfixed

+0

@nightfixed我不明白你的意思。 – ramaral

1

這是非常奇怪的,你得到墜毀在該行:

tv.setLayoutParams(layoutParams); 

我想你,你得到它在該行,因爲創建查看Don沒有任何LayoutParams(此外,如果它是RelativeLayout.LayoutParams):

layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 

我認爲,你可以通過這個視頻從的DevBytes提高addText()方法: http://www.youtube.com/watch?v=55wLsaWpQ4g

但是,如果你想使你的代碼工作,你需要更換你這樣的步驟:

TextView tv = new TextView(cx); 
tv.setText(textParam); 
tv.setTextColor(Color.WHITE); 
this.addView(tv); // Programatically created View get LayoutParams when we add it to a ViewGroup 

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams(); 
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 
tv.setLayoutParams(layoutParams); 

或自創建佈局PARAMS:

TextView tv = new TextView(cx); 
tv.setText(textParam); 
tv.setTextColor(Color.WHITE); 

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 
tv.setLayoutParams(layoutParams); 

this.addView(tv); 
1

我覺得你得到一個NPE因爲tv.getLayoutParams();回報null

嘗試以下操作:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 

tv.setLayoutParams(layoutParams); // the app crashes when I add this line 

this.addView(tv);