解决方案是将自定义视图包装在填充整个屏幕的RelativeLayout中,然后使用addContentView添加它.例如,要在屏幕底部添加自定义按钮:

// Fake empty container layout

RelativeLayout lContainerLayout = new RelativeLayout(this);

lContainerLayout.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT ));

// Custom view

Button mCustomView = new Button(this);

mCustomView.setText("Test");

LayoutParams lButtonParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );

lButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

mCustomView.setLayoutParams(lButtonParams);

lContainerLayout.addView(mCustomView);

// Adding full screen container

addContentView(lContainerLayout, new LayoutParams( LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT ) );

这应该成功

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐