2013-07-16 28 views
0

我的要求是,我希望以垂直方式動態顯示三個視圖(即ImageView,TextView,TextView)。想要在android中動態使用垂直方式顯示三個視圖

我確實喜歡下面的程序。但它不是working.Can任意一個幫助me.Please看到下面的代碼

for (int i = 0; i < 10; i++) { 
    final RelativeLayout relativeLayout=new RelativeLayout(this); 
    final ImageView imageView=new ImageView(this); 
    imageView.setId(100+i); 
    imageView.setBackgroundResource(R.drawable.ic_launcher); 
    final RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    relativeLayout.post(new Runnable() { 

     @Override 
     public void run() { 
      relativeLayout.addView(imageView);    
     } 
    }); 
    final TextView textView=new TextView(this); 
    textView.setId(200+i); 
    String data="Click"+"\n"+"Show"; 
    textView.setText("click"); 


    newParams.addRule(RelativeLayout.BELOW, imageView.getId()); 
    newParams.addRule(RelativeLayout.ALIGN_LEFT, imageView.getId()); 
    relativeLayout.post(new Runnable() { 

     @Override 
     public void run() { 
      relativeLayout.addView(textView, newParams);     
     } 
    }); 
    final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    final TextView text=new TextView(this); 
    text.setId(200+i); 
    //String data="Click"+"\n"+"Show"; 
    text.setText("data"); 


    params.addRule(RelativeLayout.BELOW, textView.getId()); 
    params.addRule(RelativeLayout.ALIGN_LEFT, textView.getId()); 
    relativeLayout.post(new Runnable() { 

     @Override 
     public void run() { 
      relativeLayout.addView(text, params);    
     } 
    }); 
     linearLayout.post(new Runnable() { 

     @Override 
     public void run() { 
      linearLayout.addView(relativeLayout); 

     } 
    }); 
    } 
+0

你有沒有考慮過使用方向設置爲垂直的'LinearLayout'? –

+0

如何設置垂直線性佈局使用代碼。現在感謝我得到了輸出適當的 – user2587727

+0

首先你可以發佈圖像的某個地方向我們展示什麼是你的記憶,其次是這種情況ListView更好... – Selvin

回答

0

考慮使用LinearLayout設置爲垂直方向。我發現它們更容易以編程方式用於這樣的事情。

下面是一個片的示例代碼:

LinearLayout linearLayout = new LinearLayout(this); 
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
linearLayout.setOrientation(LinearLayout.VERTICAL); 

linearLayout.addView(childView1); 
linearLayout.addView(childView2); 
linearLayout.addView(childView3); 

rootView.addView(linearLayout); 

childView1childView2childView3將垂直出現。

當然,您需要相應地修改您的代碼。

相關問題