android 在代码中如何设置 根视图的宽高?

android 在代码中如何设置 根视图的宽高?

如何设置视图container的宽高?

public void popEdit(EditText _edit)
{ //

edit=_edit;

LinearLayout container=new LinearLayout(cont);

        container.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams lp1 =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT-80, LinearLayout.LayoutParams.WRAP_CONTENT);    
        lp1.weight=1;
        EditText content=new EditText(cont);
        content.setHint("写点什么吧...");
        //content.setLayoutParams(lp1);
        container.addView(content, lp1);
        Button button=new Button(cont);

        button.setBackgroundDrawable(cont.getResources().getDrawable(R.drawable.bton4));
        container.addView(button);

        pop = new PopupWindow(container, edit.getWidth(), LayoutParams.WRAP_CONTENT, true);
        pop.setTouchable(true);
        pop.setBackgroundDrawable(new BitmapDrawable()); //
        pop.setFocusable(true);
        pop.showAsDropDown(edit, 0, 0);//位于et下面


  }

在上面的代码中,根视图的宽高是通过LinearLayout.LayoutParams来设置的。其中,setOrientation方法设置了LinearLayout的布局方向,LinearLayout.LayoutParams.FILL_PARENT-80设置了宽度为填充父窗口减去80,LinearLayout.LayoutParams.WRAP_CONTENT设置了高度为包裹内容。


视图container的宽高是在PopupWindow构造器中设置的,宽度为edit.getWidth()(即EditText的宽度), 高度为LayoutParams.WRAP_CONTENT(即包裹内容)。