如何将ViewPager的标题粘贴到TOP。 PagerTitleStrip Android

时间:2013-04-19 21:26:55

标签: android android-viewpager

我对ViewPager有一些奇怪的问题。

我需要将PagerTitleStrip添加到我的ViewPager并将其放在首位。我尝试将layout_gravity =“top”,它在“bottom”上为它保留空间并在“top”上显示标题字符串!

当我将重力放在“底部”时,它会正确显示。

这是屏幕

top - http://clip2net.com/s/4WDDUJ

底部 - http://clip2net.com/s/4WDD8k

ViewPager - 绿色 PagerTitleStrip - 蓝色 寻呼机内容match_parrent - 红色

请帮助我理解它为什么会发生。如何贴在顶部,底部没有绿色空间?

谢谢。

这是寻呼机的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/pagerTelefoonInfo"
    android:layout_width="wrap_content"
    android:background="#1AF00F"
    android:layout_height="wrap_content" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pagerTitleStrip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#3AFFEF"
        android:layout_gravity="top" >
    </android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
</LinearLayout>

这是活动:

public class APager extends Activity {

ViewPager pager;
PagerAdapter pagerAdapter;

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

    pager = (ViewPager) findViewById(R.id.pager);
    pagerAdapter = new MyPagerAdapter();
    pager.setAdapter(pagerAdapter);


}


private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 1;
    }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;
        switch (position) {
        case 0:
            view = inflater.inflate(R.layout.fragment1, null);
            ((ViewPager) collection).addView(view, 0);
            break;
        case 1:
            view = inflater.inflate(R.layout.fragment2, null);
            ((ViewPager) collection).addView(view, 0);
            break;
        }

        return view;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "Title " + position;
    }

    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    public Parcelable saveState() {
        return null;
    }
    // public int getItemPosition(Object object) { return POSITION_NONE; }
}

}

1 个答案:

答案 0 :(得分:0)

看一下viewPager布局设置我可以正确使用顶部的条带:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          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="0px"
    android:layout_weight="1">

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"/>
</android.support.v4.view.ViewPager>