ViewPager in android is taking full screen?

两盒软妹~` 提交于 2019-12-23 07:03:43

问题


In my xml file I have a Linear layout that has a ViewPager for showing images and another Linear layout that contains previous and Next buttons for selecting images.My xml looks like this:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/goto_first"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="first" >
            </Button>

            <Button
                android:id="@+id/goto_last"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="last" >
            </Button>
        </LinearLayout>

    </LinearLayout>

My problem is that the ViewPager is taking full screen and the Linearlayout with previous next buttons is not showing up because there is no space left to draw this LinearLayout.

I am using Fragments to populate ViewPager with views.The xml file for the fragment view that goes into ViewPager is:

<ImageView
    android:id="@+id/ItemImage"
    android:layout_width="320dp"
    android:layout_height="180dp" />

<TextView
    android:id="@+id/ItemText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|bottom"
    android:layout_marginBottom="5dp"
    android:textColor="#ffffffff"
    android:textStyle="bold" />
</FrameLayout>

The output i am getting after rendering is in this manner:
1.image ( covering 45% screen height)
2.blank space (covering 45% screen height)
3.Textview (covering rest of the 10% screen height)

The output i want is :
1.image ( covering 45% screen height)
2.Textview (covering 10% screen height)
3.LinearLayout for Buttons (covering rest of the 45% screen height)


回答1:


Use RelativeLayout. Add your buttons to the bottom with alignParentBottom="true". Then add the ViewPager with layout_above="@id/buttons"




回答2:


Just give those to viewpager:

 android:layout_height="0dp"
 android:layout_weight="1"



回答3:


The problem with viewpagers are they don't know the size since each fragment could possibly be a different size. You have to manually set the viewpager's, not wrap_content. I recommend setting a weight to all the items in the outer linear layout.




回答4:


Just use a RelativeLayout and use the attributes like `

android:layout_below

until you achieve the position you desire. Relative Layout is your friend in these cases.




回答5:


Just Do The Following to make room for other views, below or above:

< android.support.v4.view.ViewPager **android:layout_width**="match_parent" **android:layout_height**="match_parent" **android:layout_weight**="1"/>

This answer was taken from a comment made by Jave from the answers given on the below link.

Link: "Android ViewPager dimension"




回答6:


public class WrapViewPager extends ViewPager {

    public WrapViewPager(Context context) {
        super(context);
    }

    public WrapViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height) height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}



回答7:


In my case, this code works:

<androidx.viewpager.widget.ViewPager
    android:id="@+id/myviewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" />


来源:https://stackoverflow.com/questions/13966904/viewpager-in-android-is-taking-full-screen

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