如何以编程方式设置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);
但按钮仍在左下角。
答案 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