【问题标题】:How to stick Title for ViewPager to the TOP. PagerTitleStrip Android如何将 ViewPager 的标题粘贴到顶部。 PagerTitleStrip Android
【发布时间】:2013-04-13 09:14:01
【问题描述】:

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

我需要将 PagerTitleStrip 添加到我的 ViewPager 并将其放在顶部。我尝试放置 layout_gravity = "top",它在 "bottom" 上为其保留空间并在 "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; }
}

}

【问题讨论】:

    标签: android android-viewpager


    【解决方案1】:

    看看我设置的 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>
    

    【讨论】:

    • 刚做了和你一样的布局,仍然一样。。此外,我添加了填充 14dp,看看会发生什么clip2net.com/s/4WDPfm 它创建了 2 个标题...在顶部和底部,这是 ViewPager 的一部分(((
    • 你能发布你的代码来设置viewpager吗?
    • xml 内容(fragment1 和 fragment2)只是线性布局 match_parrent
    • 不要使用 pagerAdapter,而是尝试使用 support v4 包中的 FragmentStatePagerAdapter。
    • 使用 .addView(view) 解决的问题;而不是 .addView(view, 0); p.s.在使用 FragmentStatePagerAdapter 的情况下,如何访问我活动中所有 fragemtns 的所有视图?无论如何谢谢你的帮助))
    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2015-02-13
    相关资源
    最近更新 更多