2015-04-05 53 views
1

我想製作一個按鈕,以便按第一個框出現在按鈕下方,第二個到第一個右側,第3月1日在下面等,有點像這樣:用於以類似網格的方式添加框的按鈕

[Button] 
[1st][2nd] 
[3rd][4th] 
[5th] ... 

我有一些代碼,從一些不錯的球員,但同時它確實增加了他們的意見得到堆疊在彼此的頂部,而不是在一個方式我如上所示。有人可以幫助我嗎?

下面是代碼:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

public class teamCreateScreen extends Activity { 

    int i = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.team_locate_layout); 
    } 

    public void createTeam(View view) { 
     final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam); 
     RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     Button tv = new Button(getApplicationContext()); 
     if (tv.getId() > 0) { 
      relativeParams.addRule(RelativeLayout.BELOW, tv.getId()); 
     } 
     tv.setText("New Team"); 
     tv.setId(i); 
     rlTeam.addView(tv, relativeParams); 
     i++; 
    } 
} 

而且在其中添加的XML佈局的觀點:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/rlTeam"> 

    <Button 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:id="@+id/teamAddBtn" 
     android:text="+" 
     android:textSize="30sp" 
     android:onClick="createTeam"/> 

</RelativeLayout> 
+0

替換relativeParams.addRule(RelativeLayout.BELOW,tv.getId());與relativeParams.addRule(RelativeLayout.BELOW,view.getId()); – 2015-04-05 20:32:04

+0

做到了,沒有任何改變:/ – Richard 2015-04-05 20:35:11

+0

我可以問你爲什麼要這麼做嗎? – 2015-04-05 20:48:34

回答

0

我認爲,這是例如做最好的事情Android GridView

無論如何嘗試這一代碼:

public class MainActivity extends ActionBarActivity { 

private int counter = 0; 
private int lastId = -1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

public void createTeam(View view) { 
    final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam); 
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    Button tv = new Button(getApplicationContext()); 

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 

     tv.setId(Utils.generateViewId()); 

    } else { 

     tv.setId(View.generateViewId()); 

    } 

    if(lastId == -1) { 
     relativeParams.addRule(RelativeLayout.BELOW, view.getId()); 
     counter++; 
    } else if(counter == 1) { 
     relativeParams.addRule(RelativeLayout.RIGHT_OF, lastId); 
     relativeParams.addRule(RelativeLayout.ALIGN_TOP, lastId); 
     counter++; 
    } else if(counter == 2) { 
     relativeParams.addRule(RelativeLayout.BELOW, lastId); 
     counter = 1; 
    } 


    tv.setText("New Team"); 

    lastId = tv.getId(); 

    rlTeam.addView(tv, relativeParams); 
} 

的utils的類:

import java.util.concurrent.atomic.AtomicInteger; 

/** 
* Created by Hema on 05/04/2015. 
*/ 
public class Utils { 
    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1); 

    /** 
    * Generate a value suitable for use in {@link #setId(int)}. 
    * This value will not collide with ID values generated at build time by aapt for R.id. 
    * 
    * @return a generated ID value 
    */ 
    public static int generateViewId() { 
     for (;;) { 
      final int result = sNextGeneratedId.get(); 
      // aapt-generated IDs have the high byte nonzero; clamp to the range under that. 
      int newValue = result + 1; 
      if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. 
      if (sNextGeneratedId.compareAndSet(result, newValue)) { 
       return result; 
      } 
     } 
    } 
} 

我想向你展示它爲什麼不工作: 當您創建視圖這樣Button tv = new Button(getApplicationContext());如果調用tv.getId()將等於-1,直到你打電話tv.setId(int id)。

+0

非常感謝! :) – Richard 2015-04-05 21:16:26

+0

不客氣,但你必須閱讀關於GridView和TableLayout – 2015-04-05 21:18:47

+0

哇,實際上有一個小問題。當視圖被添加時,它們會彼此相加。請看看這個問題,我已經展示了他們需要如何添加(網格)。如果可能,你可以做一些調整嗎? – Richard 2015-04-05 21:19:50

相關問題