2013-06-21 62 views
0

我想用相對佈局添加不同的視圖。該的EditText和整流器得到補充,但列表視圖沒有顯示。還有,我不是用個XML佈局編程的相對佈局位置

public class CustomView extends LinearLayout { 
    Context rContext; 
    private String rTitle; 

    private Spinner rSpinner; 
    private EditText rInput; 
    private ListView rResultsList; 

    public CustomView(Context context) { 
     super(context); 
     rContext = context; 
    } 

    public CustomView(Context context, AttributeSet attrs, int theme) { 
     super(context, attrs, theme); 
    } 

    public void initialize(String title) { 

     rTitle = title; 
     rInput = new EditText(rContext); 
     rInput.setId(1); 
     rSpinner = new Spinner(rContext); 
     rSpinner.setId(2); 
     rResultsList = new ListView(rContext); 
     rResultsList.setId(3); 
     rInput.setText("1"); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(rContext, R.layout.result_item, R.id.result_item, new String[] { "this", "that" }); 
     rResultsList.setAdapter(adapter); 
     addViews(); 
    } 

    @Override 
    public String toString() { 

     return rTitle; 
    } 

    private void addViews() { 

     RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(ConvertoActivity.APP_WIDTH/2, 100); 
     relativeParams.addRule(RelativeLayout.RIGHT_OF, rInput.getId()); 

     RelativeLayout.LayoutParams relativeParams2 = new RelativeLayout.LayoutParams(ConvertoActivity.APP_WIDTH/2, 100); 
     relativeParams.addRule(RelativeLayout.LEFT_OF, rSpinner.getId()); 

     RelativeLayout.LayoutParams relativeParams3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     relativeParams.addRule(RelativeLayout.BELOW, rInput.getId()); 

     this.addView(rInput, relativeParams2); 
     this.addView(rSpinner, relativeParams); 
     this.addView(rResultsList, relativeParams3); 


    } 
} 

enter image description here

回答

0

沒關係,我找到了答案。 我正在將規則添加到相關參數的相同對象。

0

您的自定義視圖擴展的LinearLayout和你正在使用RelativeLayout.Layout PARAMS。改變你的自定義視圖來擴展RelativeLayout。

+0

我試過了。當我擴展相對佈局時,它只顯示列表視圖,而不顯示輸入視圖和微調器 –

+0

這可能是因爲您正在使用三個不同的佈局參數實例。如果要垂直顯示每個視圖組件,則可以簡單地將高度爲每個項目的WRAP_CONTENT的垂直佈局設置爲垂直。 – deepdroid

+0

沒有幫助:(試過了 –