【问题标题】:TitlePageIndicator: Title text not appearing or hidden on top of activity layout.(Viewpager,Xml)TitlePageIndicator:标题文本未出现或隐藏在活动布局顶部。(Viewpager,Xml)
【发布时间】:2012-08-29 23:21:11
【问题描述】:

ViewPagerFragment.java

package nsixty.crew.app;

import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitleProvider;

public class ViewPagerFragment extends FragmentActivity {

    private CustomFragmentAdapter mPagerAdapter;

    ViewPager pager;
    TitlePageIndicator titleIndicator;

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

        this.initialisePaging();

    }

    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();

        fragments
                .add(Fragment.instantiate(this, LoginActivity.class.getName()));
        fragments.add(Fragment.instantiate(this, main.class.getName()));
        this.mPagerAdapter = new CustomFragmentAdapter(
                super.getSupportFragmentManager(), fragments);
        pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);
        titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
        titleIndicator.setViewPager(pager);

    }

    public static class CustomFragmentAdapter extends FragmentPagerAdapter
            implements TitleProvider {
        public static final int POSITION_PAGE_1 = 0;
        public static final int POSITION_PAGE_2 = 1;
        public static final int POSITION_PAGE_3 = 2;
        private List<Fragment> fragments;
        private static final String[] TITLES = new String[] { "LOGIN", "HOME" };

        public static final int NUM_TITLES = TITLES.length;

        @Override
        public String getTitle(int position) {
            return TITLES[position];
        }

        public CustomFragmentAdapter(FragmentManager fm,
                List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public int getCount() {
            // return this.fragments.size();
            return NUM_TITLES;
        }

        @Override
        public Fragment getItem(int position) {

            try {
                LoginActivity.class.newInstance();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return this.fragments.get(position);

        }

    }

}

lcmeter.xml

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


    <com.viewpagerindicator.TitlePageIndicator
        android:id="@+android:id/titles"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+android:id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nsixty.crew.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <support-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="false" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:icon="@drawable/icon87"
        android:label="@string/app_name" >
        <activity
            android:name="ViewPagerFragment"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".main" >
            <intent-filter>
                <action android:name="nsixty.crew.app.main" />

                <category
                    android:name="android.intent.category.DEFAULT"
                    android:screenOrientation="portrait" />
            </intent-filter>
        </activity>
        <activity
            android:name=".play"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="nsixty.crew.app.play" />

                <category
                    android:name="android.intent.category.DEFAULT"
                    android:screenOrientation="landscape" />
            </intent-filter>
        </activity>
        <activity
            android:name=".secondView"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="nsixty.crew.app.SECONDVIEW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".recorder"
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="nsixty.crew.app.RECORD" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".send"
            android:label="@string/app_name" >

            <!-- android:theme="@android:style/Theme.Dialog" -->
            <intent-filter>
                <action android:name="nsixty.crew.app.SHARE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".gallery"
            android:theme="@android:style/Theme.Dialog" >
            <intent-filter>
                <action android:name="nsixty.crew.app.gallery" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegisterActivity"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
        <activity
            android:name=".media"
            android:theme="@android:style/Theme.Dialog" >
            <intent-filter>
                <action android:name="nsixty.crew.app.media" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".LoginActivity" >
        </activity>
        <activity android:name="CustomTitleBar" >
        </activity>
    </application>

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.WRITE_SETTINGS" >
    </uses-permission>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <uses-feature android:name="android.hardware.camera" />

</manifest>

屏幕截图1

ScreenShot2

请帮我解决这些小问题,我就是不知道问题出在哪里,我已经为此工作了好几个小时。我认为问题将来自 Xml 布局谢谢。

【问题讨论】:

    标签: android android-layout android-viewpager android-xml viewpagerindicator


    【解决方案1】:

    试试这样:

      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager);
    
        // Create our custom adapter to supply pages to the viewpager.
        mPagerAdapter = new Pager_Adapter(this,getSupportFragmentManager());
        mViewPager = (ViewPager)findViewById(R.id.pager);
        mViewPager.setAdapter(mPagerAdapter);
    
        // Start at a custom position
        mViewPager.setCurrentItem(0);
    
         // Find the indicator from the layout
        mTabs = (TitleIndicator) findViewById(R.id.swipeytabs);
        mTabs.setAdapter(mPagerAdapter);
    
        // Set the indicator as the pageChangeListener
        mViewPager.setOnPageChangeListener(mTabs);
    
    }
    
    class Pager_Adapter extends FragmentPagerAdapter implements ViewpagerAdapter{
    
    
        private final Context mContext;
    
        public Pager_Adapter(Context context, FragmentManager fm) {
            super(fm);
    
            this.mContext = context;
        }
    
    
        @Override
        public Fragment getItem(int pos) {
    
            return ItemFragment.newInstance(QUES[pos],listArray[pos]);
            //return ItemFragment.newInstance(QUES[pos % TITLES.length]);
    
        }
    
        @Override
        public int getCount() {
            return TITLES.length;
        }
    
        public TextView getTab(final int position, TitleIndicator root) {
            TextView view = (TextView) LayoutInflater.from(mContext).inflate(
                    R.layout.swipey_tab_indicator, root, false);
    
                                 //  swipey_tab_indicator is the your titleBar Layout ,
                                  //  You can use only textview on that layout for the titlebar
            view.setText(TITLES[position]);
            view.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    mViewPager.setCurrentItem(position);
    
                }
            });
            return view;
        }
    
    }
    

    有关更多信息,请查看此 link

    【讨论】:

    • 您好 Patel 感谢您的回复,您的代码适用于标签页指示器正在做标题页指示器,请您编辑标题页指示器。谢谢
    • 请检查给定的链接,这个链接包含Title + Tabindicator.Good Luck。
    【解决方案2】:

    我使用了这个库View pager extensions,其中我使用了滑动标签视图指示器而不是标题页指示器,它工作得很好。

    【讨论】:

      【解决方案3】:

      嗯,这个文本的默认选择颜色似乎是黑色,而你的背景是,所以你应该在 android XML 文件中将其更改为另一种颜色。

      【讨论】:

        猜你喜欢
        • 2014-09-29
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 2021-07-25
        • 2011-06-07
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        相关资源
        最近更新 更多