我想製作一個按鈕,以便按第一個框出現在按鈕下方,第二個到第一個右側,第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>
替換relativeParams.addRule(RelativeLayout.BELOW,tv.getId());與relativeParams.addRule(RelativeLayout.BELOW,view.getId()); – 2015-04-05 20:32:04
做到了,沒有任何改變:/ – Richard 2015-04-05 20:35:11
我可以問你爲什麼要這麼做嗎? – 2015-04-05 20:48:34