简体   繁体   English

如何在Relativelayout usigin Java代码中一个接一个地创建按钮?

[英]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. 问题是,当运行该应用程序时,会一个接一个地创建按钮。

2个按钮(Chilena和cuenta车辙)

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.

相关问题 我想将标签和文本字段彼此对齐,并将所有按钮排成一行; 我该怎么做? - I want to align labels and textfields next to one other and all the buttons in a single row; how can i do it? 动态添加彼此相邻的按钮 - RelativeLayout - Dynamically adding buttons next to each other - RelativeLayout 我如何在RelativeLayout中获得不相互重叠的按钮 - How do I get buttons not to sit on top of each other in RelativeLayout ANDROID-如何在RelativeLayout中将多个Button水平居中? - ANDROID - How can I center horizontally multiple Buttons in RelativeLayout? 如何按照说明在一个 JFrame 中制作 Java 中的六个按钮? - How can I make six buttons in Java inside one JFrame according to the instructions? 如何在Android中的RelativeLayout中以编程方式在按钮旁边显示文本视图? - How can i display programmatically a textview next to a button in a RelativeLayout in Android? 如何创建一个相邻的JFrame? - How to create JFrame one next to other? 我可以在Java中创建一个没有标题按钮的窗口吗? - Can I create a Window in Java with no caption buttons? 如何使用Java创建一个文件夹并将一个文件放入Dropbox中? - How can I create a folder and put one file inside it in Dropbox using java? Java Swing - 如何使用next和previous按钮显示数组中对象的变量? - Java Swing - How can I make next and previous buttons to display variables of objects in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM