سؤال

I have an activity, called CollectionActivity, with a viewpager. The viewpager has it owns adapter, PagerAdapter: CollectionPagerAdapter extends PagerAdapter.

The important instantiateItem method:

@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;
}

The constructor in the class Report (extends LinearLayout) looks like:

    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);

The problem; the view I´m getting from R.layout.activity_report shows up with a really tiny width on the screen. The Button below it however, get the width it needs.

activity_report.xml has the width and height of fill parent, and starts with a horizontal layout filled with textboxes, buttons and other stuff. Activity_report.xml looks great on the preview in eclipse, and has looked fine when I used it to an activity before.

Why cant I use this.addView(view) to add my layout activity_report.xml and getting it on the whole screen? (not with a tiny width as it is now)

هل كانت مفيدة؟

المحلول 2

For some reason, everything worked when I had no images lying by themselves in my view xml file. I simply put each image in their own linear layout - and I could now see the whole layout on the whole screen.

I have no idea why this was the issue. If you know, leave a comment!

نصائح أخرى

When you use addView(View) you have to specify the LinearLayout.LayoutParams of the parent view.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top