如何以编程方式在edittext的右上角设置按钮?

时间:2013-12-22 10:09:04

标签: android button android-edittext android-framelayout layoutparams

如何以编程方式设置EditText右上角的按钮?

FrameLayout fl = new FrameLayout(this);
lv.addView(fl);

searchTxt = new EditText(this);
fl.addView(searchTxt);

btnSearch = new Button(this);           

我试过这个

btnSearch.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
fl.addView(btnSearch);

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
param.gravity = Gravity.RIGHT;
fl.addView(btnSearch, param);

但按钮仍在左下角。

2 个答案:

答案 0 :(得分:1)

您需要为按钮设置布局参数,如下所示:

FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
param.gravity = Gravity.RIGHT;

并将其设为按钮

btnSearch.setLayoutParam(param);
f1.addView(btnSearch);

答案 1 :(得分:0)

layoutparams

中将Fill_Parent设置为LinearLayout为 width