在LinearLayout中通过编程方式设置两个按钮之间的间距

13

如何以编程方式在“regler”按钮和“decommender”按钮之间设置20dp的边距。

输入图像描述

这是我的工作方法

 LinearLayout lytmain = new LinearLayout(Mcontex);
            lytmain.setOrientation(LinearLayout.VERTICAL);
            LinearLayout lytdate = new LinearLayout(Mcontex);
            LinearLayout lytbutton = new LinearLayout(Mcontex);
            lytbutton.setBackgroundResource(R.color.black);



            lytbutton.setBackgroundResource(R.color.black);
            lytdate.setBackgroundResource(R.color.black);
            lytmain.setBackgroundResource(R.color.black);
            Button btnset = new Button(Mcontex);
            Button btncancel = new Button(Mcontex);


            btncancel.setShadowLayer(2, 1, 1, R.color.black);
            btnset.setShadowLayer(2, 1, 1, R.color.black);
            btnset.setBackgroundResource(R.drawable.black_button);
            btncancel.setBackgroundResource(R.drawable.black_button);

            btnset.setTextColor(Mcontex.getResources().getColor(R.color.white));
            btncancel.setTextColor(Mcontex.getResources().getColor(R.color.white));
            btncancel.setTextSize(15);
            btnset.setTextSize(15);
            btnset.setText("Régler");
            btncancel.setText("Décommander");

            btnset.setGravity(Gravity.CENTER);
            btncancel.setGravity(Gravity.CENTER);

            final WheelView month = new WheelView(Mcontex);
            final WheelView year = new WheelView(Mcontex);
            final WheelView day = new WheelView(Mcontex);

            lytdate.addView(day, new LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.2f));
            lytdate.addView(month, new LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.8f));

            lytdate.addView(year, new LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
            requestWindowFeature(Window.FEATURE_NO_TITLE);



            lytbutton.addView(btnset, new LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,1.5f));

            lytbutton.addView(btncancel, new LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1.5f));


            lytbutton.setPadding(5, 5, 5, 5);
            lytmain.addView(lytdate);
            lytmain.addView(lytbutton);

            setContentView(lytmain);

            getWindow().setLayout(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);

有很多更有效的方法来选择日期,而不是这个iOS风格的“东西”;-) - Waza_Be
请访问https://github.com/Prototik/HoloEverywhere 或 https://github.com/SimonVT/android-datepicker查看有关编程的相关内容。 - Waza_Be
@Waza_Be:当然亲爱的,但现在这个iOS问题是我的问题。谢谢你的例子 :) - Dimitri
@Dimitri:我只是好奇为什么你想用代码构建完整布局而不使用XML布局文件。 - Squonk
@squonk:这只是朋友之间的挑战,我需要完成最后一步才能赢得胜利。 - Dimitri
显示剩余2条评论
1个回答

41

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接