如何在ViewPager中居中固定大小的视图?

时间:2013-06-12 13:46:53

标签: android android-viewpager

我在Android上遇到了一个非常基本的问题我过去几个小时一直在苦苦挣扎。 我无法在其容器中创建ViewPager视图内容中心。这是我的代码:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:background="#FF0000FF">

    ...

    </LinearLayout>

</FrameLayout>

所以视图在我的ViewPager中正确显示,但它不会居中。通过在FrameLayout上设置颜色,它似乎包裹LinearLayout而不是做马赫父母。我尝试了RelativeLayoutLinearLayout,没有太多机会,任何想法?

1 个答案:

答案 0 :(得分:0)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:background="#FF0000FF">

    ...

    </LinearLayout>

</RelativeLayout>

试试这个