5

我在应用程序中使用了支持操作栏并在 2 台设备 Nexus S 和 Nexus 7 中进行了测试,我发现关于标签栏宽度、Nexus S 标签栏填充宽度的不同结果,而 nexus 7 标签栏在左侧留下了一些空间。我使用了支持操作栏提供的默认主题,应用自定义主题不会影响操作栏,因为父样式必须是 Theme.AppCompat.Light

清单中的活动

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light" > 

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

班级:

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

    ActionBar bar;
    ViewPager pager;
    TabsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pager = (ViewPager) findViewById(R.id.pager);
        bar = getSupportActionBar();

        adapter = new TabsAdapter(getSupportFragmentManager());

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        bar.setHomeButtonEnabled(false);

        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the
                // corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have
                // a reference to the
                // Tab.
                bar.setSelectedNavigationItem(position);
            }
        });

        Tab hometab = bar.newTab();
        hometab.setText("Home");
        hometab.setTabListener(this);
        bar.addTab(hometab);
        Tab cameratab = bar.newTab();
        cameratab.setText("Camera");
        cameratab.setTabListener(this);
        bar.addTab(cameratab);
        Tab exploretab = bar.newTab();
        exploretab.setText("Explore");
        exploretab.setTabListener(this);
        bar.addTab(exploretab);

        bar.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
            // TODO Auto-generated method stub
    }

    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
        pager.setCurrentItem(arg0.getPosition());
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
    }

}

这是屏幕截图

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

2

为平板电脑使用高分辨率图像。将其放在 drawable-xxhdpi 文件夹中。如果此解决方案不起作用。那么

替代方法:

脚步

1:创建 3 个单独的布局——一个用于主页选项卡,一个用于相机选项卡,一个用于探索选项卡。

2:使用 Layout Inflater ,您可以将此布局单独添加到这些选项卡中。

示例代码:

public class MyActivities extends SherlockFragmentActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.customActionBarTabStyle);
    super.onCreate(savedInstanceState);

}

public static class TabListener<T extends Fragment> implements
        ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity
     *            The host Activity, used to instantiate the fragment
     * @param tag
     *            The identifier tag for the fragment
     * @param clz
     *            The fragment's Class, used to instantiate the fragment
     */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;

    }

    public void onTabSelected(Tab tab, FragmentTransaction ignoredFt) {
        FragmentManager fragMgr = ((FragmentActivity) mActivity)
                .getSupportFragmentManager();
        FragmentTransaction ft = fragMgr.beginTransaction();
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        // ft.add(android.R.id.content, mFragment, mTag);
        ft.add(R.id.relMiddle, mFragment, mTag);
        ft.addToBackStack(mTag);
        ft.commit();

    }

    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            FragmentManager man = ((FragmentActivity) mActivity)
                    .getSupportFragmentManager();
            if (man.getBackStackEntryCount() > 0)
                man.popBackStack(man.getBackStackEntryAt(0).getName(),
                        FragmentManager.POP_BACK_STACK_INCLUSIVE);
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

public void settings() {
    actionbar = getSupportActionBar();
    actionbar.setDisplayShowTitleEnabled(false);
    getSupportActionBar().setCustomView(R.layout.myactivities_header);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    /*
     * For showing custom top layout in actionbar
     * 
     * Build verson is checking for Api <3.0
     */
    View homeIcon = findViewById(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.id.home
            : R.id.abs__home);
    ((View) homeIcon.getParent()).setVisibility(View.GONE);

    mTabsAdapter = new TabsAdapter(this, actionbar, mViewPager);

    /*
     * Use Customview for TABS IN FULL WIDTH
     * 
     * 1: Note: use imageview with full width and full height.
     * 
     * 2: Note : If we use seticon instead setContentview,Tabs may not show
     * in full width /height
     */

    mTabsAdapter.addTab(
            actionbar.newTab().setText(" ")
                    .setCustomView(R.layout.cus_home_tab),
            Home.class, null);
    mTabsAdapter.addTab(
            actionbar.newTab().setText("")
                    .setCustomView(R.layout.cus_camera_tab),
            Camera.class, null);
    mTabsAdapter.addTab(
            actionbar.newTab().setText("")
                    .setCustomView(R.layout.cus_explore_tab),
            Rxplore.class, null);

}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ViewPager.OnPageChangeListener, ActionBar.TabListener {
    private final Context mContext;
    private final ActionBar mBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(FragmentActivity activity, ActionBar bar,
            ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mBar = bar;
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);

        System.out.println("inside tab adapter");
    }

    public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss,
            Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        //System.out.println("Tabs Size is " + mTabs.size());
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
}


}
于 2013-11-20T10:08:08.517 回答