我的ViewPagerIndicator在哪里?

时间:2014-03-03 06:09:02

标签: android android-viewpager viewpagerindicator

以下是我的布局:

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

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

    <com.viewpagerindicator.LinePageIndicator
        android:id="@+id/titles"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:layout_below="@id/pager" 
        android:background="#808080"/>

</RelativeLayout>  

和我的 onCreate()基于网站上的示例代码段:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new ScreenSlideAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

        lineIndicator = (LinePageIndicator)findViewById(R.id.titles);
        lineIndicator.setViewPager(pager);


    }  

当我运行应用程序时,指示器不可见。我做错了什么?

3 个答案:

答案 0 :(得分:4)

系统从XML文件的顶部到底部读取元素,以便在系统知道ViewPagerIndicator需要在其下面之前,ViewPager正在填充整个高度。交换订单并改为使用layout_above

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

    <com.viewpagerindicator.LinePageIndicator
        android:id="@+id/titles"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:background="#808080"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/titles"  />

</RelativeLayout>  

答案 1 :(得分:3)

试试这个伙伴:

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

    <com.viewpagerindicator.LinePageIndicator
        android:id="@+id/titles"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" 
        android:background="#808080"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/titles"  />

</RelativeLayout>  

答案 2 :(得分:1)

使用此...

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

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

<com.viewpagerindicator.LinePageIndicator
    android:id="@+id/titles"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/pager"
    android:background="#808080" />

</RelativeLayout>