如何在 Relativelayout usigin Java 代码中创建一个并排的按钮?
How I can create buttons one next to the other inside Relativelayout usigin Java code?
我正在使用 java 代码
从 JSON 创建许多按钮
Relativelayout rl = (Relativelayout) findById(R.id.layout_productos);
...
JSONArray jsonArray = new JSONArray(tmp.getString("productos"));
Button bt[] = new Button[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i ++){
final float scale = getResources().getDisplayMetrics().density;
int padding_40dp = (int) (40 * scale + 0.5f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, padding_40dp);
bt[i] = new Button(DetalleServicioActivity.this);
bt[i].setText(jsonArray.getJSONObject(i).getString("nombre"));
bt[i].setTag(jsonArray.getJSONObject(i).getString("id_producto"));
bt[i].setTextColor(Color.parseColor("#FFFFFF"));
bt[i].setBackgroundColor(Color.parseColor("#D8D8D8"));
bt[i].setLayoutParams(params);
bt[i].setEnabled(false);
bt[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
rl.addView(bt[i]);
}
里面Relativelayout
<RelativeLayout
android:id="@+id/layout_productos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
</RelativeLayout>
问题是当 运行 应用程序时,按钮是一个接一个地创建的。
但我需要它们一个挨着一个放置,并且当跳线的屏幕接近边缘时才能继续
我该怎么做?
您需要添加规则才能使它们彼此相邻对齐。规则由你决定。
params.addRule(RelativeLayout.RIGHT_OF, previousViewId);
我正在使用 java 代码
从 JSON 创建许多按钮 Relativelayout rl = (Relativelayout) findById(R.id.layout_productos);
...
JSONArray jsonArray = new JSONArray(tmp.getString("productos"));
Button bt[] = new Button[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i ++){
final float scale = getResources().getDisplayMetrics().density;
int padding_40dp = (int) (40 * scale + 0.5f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, padding_40dp);
bt[i] = new Button(DetalleServicioActivity.this);
bt[i].setText(jsonArray.getJSONObject(i).getString("nombre"));
bt[i].setTag(jsonArray.getJSONObject(i).getString("id_producto"));
bt[i].setTextColor(Color.parseColor("#FFFFFF"));
bt[i].setBackgroundColor(Color.parseColor("#D8D8D8"));
bt[i].setLayoutParams(params);
bt[i].setEnabled(false);
bt[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
rl.addView(bt[i]);
}
里面Relativelayout
<RelativeLayout
android:id="@+id/layout_productos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
</RelativeLayout>
问题是当 运行 应用程序时,按钮是一个接一个地创建的。
但我需要它们一个挨着一个放置,并且当跳线的屏幕接近边缘时才能继续
我该怎么做?
您需要添加规则才能使它们彼此相邻对齐。规则由你决定。
params.addRule(RelativeLayout.RIGHT_OF, previousViewId);