2013-07-09 60 views
-1

我想創建一個方法,該方法可以動態顯示LinearLayout也位於我的Android應用程序的指定區域中,該線性佈局的數量根據給定數量。LinearLayout根據給定數字動態顯示

此外,在每個LinearLayout我想要顯示一個Button和它下面一個TextView

這裏是我已經創建的方法:

public void putLinearLayout(double number){ 

     int mButtonHeight = 100; 
     int mButtonWidth = 80; 
     LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1); 

     for(int i=1;i<=number;i++) 
     { 
      LinearLayout L = new LinearLayout(this); 


      Button b= new Button(this); 
      TextView tv = new TextView(this); 
      L.setOrientation(LinearLayout.HORIZONTAL); 
      b.setWidth(mButtonWidth); 
      b.setHeight(mButtonHeight); 
      L.addView(b); 
      L.addView(tv); 
      Linear.addView(L); 
     } 

    } 
+0

什麼是它的問題? –

+0

它不起作用 – Sally

+0

您是否向按鈕和textview添加了文本? –

回答

-1

你只需要設置layoutparams

public void putLinearLayout(double number){ 
     LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1); 
       int mButtonHeight = 100; 
       int mButtonWidth = 80; 

       for(int i=1;i<=number;i++) 
       { 
        LinearLayout L = new LinearLayout(this); 
        L.setBackgroundColor(Color.WHITE); 
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //<--this line , is what you were missing 
        L.setLayoutParams(params); //<-- and then this 
        Button b= new Button(this); 
        b.setText(i+""); 
        TextView tv = new TextView(this); 
        tv.setText("i am textview number: "+i); 
        L.setOrientation(LinearLayout.HORIZONTAL); 
        b.setWidth(mButtonWidth); 
        b.setHeight(mButtonHeight); 
        L.addView(b); 
        L.addView(tv); 
        Linear .addView(L); 
       } 

}

+0

謝謝,這是我想要的,但不完全是 – Sally

+0

問題的大小,但它的作品非常好:) – Sally

+0

我想創建一個方法,可以加載存儲在我的sqlite數據庫中的圖像。 在我的數據庫中,我創建了一個包含圖像ADRESS和圖像identifiant一個表,我的目標是把每一個圖像上已經創建 公共字符串的getImage(int i)以// afficher UNE圖像 { \t字符串資源按鈕; \t SQLiteStatement S = database.compileStatement( 「選擇」 + COL_ADRESS_IMAGE + 「FROM」 + TABLE_IMAGE + 「WHERE id_image IN(SELECT」 + COL_IMAGE_CATEGORY + \t \t \t 「FROM」 + TABLE_CATEGORY + 「WHERE」 + COL_ID_CATEGORY +「=' 「+ i +」')「); \t res = s.toString(); \t return res; } – Sally

0

我用它來繪製表格行:

public void createTableRow(String textBoxContent,Integer catId) { 
     TableLayout tl = (TableLayout) findViewById(R.id.SelectCat); 
     TableRow tr = new TableRow(this); 
     LayoutParams lp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); 
     tr.setLayoutParams(lp); 
     tr.setGravity(Gravity.RIGHT); 

     Button btnLeft = new Button(this); 
     btnLeft.setLayoutParams(new LayoutParams(0,android.view.ViewGroup.LayoutParams.WRAP_CONTENT,(float) 0.5)); 
     btnLeft.setText(R.string.Show); 
     btnLeft.setTag(catId); 
     TextView tvCenter = new TextView(this); 
     tvCenter.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1f)); 
     tvCenter.setBackgroundColor(Color.WHITE); 
     tvCenter.setText(textBoxContent); 

     tr.addView(btnLeft); 
     tr.addView(tvCenter); 

     tl.addView(tr, new TableLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); 
    } 

很簡單,如果你有一個改變以線性佈局
問題,我可以引導你更多

+0

好的,非常感謝 – Sally

+0

我有另一個問題,我想把我的數據庫中的圖像放在按鈕上,你能幫我 – Sally

+0

讀取圖像並放入byt數組,然後在數據庫中插入數組,並在加載中獲取字節數組並轉換爲位圖,這是另一個問題,你必須在另一個帖子上再次提問@Sally –