[英]How I can create buttons one next to the other inside Relativelayout usigin Java code?
I am creating many button from JSON using 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]);
}
inside Relativelayout
内部
Relativelayout
<RelativeLayout
android:id="@+id/layout_productos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
</RelativeLayout>
The problem is when is run the app, the buttons are created one upon another one. 问题是,当运行该应用程序时,会一个接一个地创建按钮。
But I need that they are located one next to the other and when the screen of a line jump approaches the edge to continue 但是我需要将它们彼此相邻放置,并且当跳线的屏幕接近边缘时才能继续
How can I do that?? 我怎样才能做到这一点??
You need to add rule in order to have them aligned next to eachother. 您需要添加规则,以使它们彼此相邻对齐。 Rules are up to you.
规则由您决定。
params.addRule(RelativeLayout.RIGHT_OF, previousViewId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.