gpt4 book ai didi

android - 从线性布局以编程方式设置两个按钮之间的边距

转载 作者:可可西里 更新时间:2023-11-01 19:09:46 25 4
gpt4 key购买 nike

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

enter image description here

这是我的作品

 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);

最佳答案

使用LinearLayout.LayoutParams

http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

您可以将 Margins 设置为 LayoutParams,然后将 LayoutParams 设置为您的 View 。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 0, 0);
button.setLayoutParams(params);

关于android - 从线性布局以编程方式设置两个按钮之间的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16552811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com