我想制作一个按钮,以这样的方式添加 View (类似盒子的东西),第一个盒子出现在按钮下方,第二个盒子出现在第一个盒子的右边,第三个盒子出现在第一个盒子下面,依此类推,有点像这样:
[Button]
[1st][2nd]
[3rd][4th]
[5th] ...
我从一些好人那里得到了一些代码,但是虽然它确实添加了 View ,但它们彼此堆叠在一起,而不是以我上面显示的方式。有人可以帮我解决这个问题吗?
这是代码:
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++;
}
}
还有添加 View 的 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>
最佳答案
我认为最好的办法是这样做,例如 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;
}
}
}
}
我想向您展示为什么它不起作用:
当您创建这样的 View 时 Button tv = new Button(getApplicationContext());
如果您调用 tv.getId() 将等于 -1,直到您调用 tv.setId(int id)。
关于java - 以类似网格的方式添加框的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29461795/