addView FILL_PARENT not working

家住魔仙堡 提交于 2019-12-09 04:34:27

Use this addView method. By the way don't use getBaseContext() unless you know why you are using it, instead use context of an activity (this).

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout);
    View view = LayoutInflater.from(this).inflate(R.layout.vitrin_layout, null);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT,
        ViewGroup.LayoutParams.FILL_PARENT); 
    parent.addView(view, params);

    setTitle(getString(R.string.title_activity_vitrin));
}
parent.addView(view, LayoutParams.FILL_PARENT);

this method was ViewGroup.addView(View child, int index) invoked actually. you need to invoke ViewGroup.addView(View child, ViewGroup.LayoutParams params) method to set a LayoutParams.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!