我有一个名为CollectionActivity的活动,带有viewpager。 viewpager拥有它的适配器,PagerAdapter:CollectionPagerAdapter扩展PagerAdapter。PagerAdapter;添加一个LinearLayout类,添加一个布局 - >小小的宽度
重要instantiateItem方法:
@Override
public Object instantiateItem(View collection, int position) {
LinearLayout lay = null;
if(position==0){
lay = new Report(context);
lay.setOrientation(LinearLayout.VERTICAL);
((ViewPager) collection).addView(lay);
}
//else for the other positions...
return lay;
}
在报告类的构造函数(扩展的LinearLayout)看起来像:
public Report(Context context) {
super(context);
this.context = context;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_report, null);
this.addView(view);
Button button = new Button(context);
button.setText("LinearLayoutExtended");
this.addView(button);
的问题;我从R.layout.activity_report获得的视图在屏幕上显示出一个非常小的宽度。然而,它下面的按钮,获得它所需要的宽度。
activity_report.xml具有填充父项的宽度和高度,并以水平布局开始,填充文本框,按钮和其他内容。 Activity_report.xml在eclipse中的预览中看起来很棒,而且在之前将它用于活动时看起来很好。
为什么不能使用this.addView(view)添加我的布局activity_report.xml并在整个屏幕上获取它? (不是现在的小宽度)
没有XML文件,这是一个猜谜游戏。我可以假设,虽然Report类的默认高度是'match_parent'。您似乎没有在构造函数中传递任何布局属性。 –