3

我试图让选项卡出现在主操作栏上,就像它们在Android 开发者网站上显示的那样

在此处输入图像描述

我写了这个基本代码:

package com.example.test;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity implements ActionBar.TabListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab tab = actionBar.newTab().setTabListener(this).setText("TAB LEFT");
        actionBar.addTab(tab);

        tab = actionBar.newTab().setTabListener(this).setText("TAB CENTER");
        actionBar.addTab(tab);

        tab = actionBar.newTab().setTabListener(this).setText("TAB RIGHT");
        actionBar.addTab(tab);

        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

}

但它显示了这一点(选项卡被拆分为堆叠的操作栏):

在此处输入图像描述

该网站指出:

...系统针对不同的屏幕尺寸调整操作栏选项卡——当屏幕足够宽时将它们放置在主操作栏中,或者当屏幕太窄时将它们放置在单独的栏中(称为“堆叠操作栏”) ,如图 9 和 10 所示。

我认为屏幕足够宽,可以将整个操作栏放在主栏上。我尝试只定义一个选项卡,但它仍然有自己的堆叠条。
我有 API Demos,但我找不到他们提到的“Honeycomb Gallery”示例,所以我可以检查他们的代码。
我还尝试将 ActionBar 导航模式设置为 STANDARD 和 LIST,但无济于事。

4

2 回答 2

0

看来操作栏选项卡策略去年更改为始终以纵向模式堆叠所有屏幕尺寸的选项卡(并且在下意识为什么它对他们起作用之前,请进一步阅读!)即使它可能适合动作栏。这似乎没有很好的记录(或我能找到的所有记录)。如果目标 API 版本是 JB,我可以确认这个新策略肯定会应用于 JB 设备。

在源代码树的提交说明中找到了这个

源代码树中的 ActionBar 选项卡策略提交注释

操作栏选项卡现在在所有屏幕尺寸上以纵向模式堆叠,而不是嵌入显示。这只会影响 targetSdkVersion 为 JB 或更高版本的应用程序,因为较旧的应用程序可能无法为条形的不同测量做好准备,或者没有适当的堆叠条形背景可绘制。

堆叠的操作栏选项卡现在有宽度限制。这可以防止可以跨越整个屏幕的超宽选项卡。如果选项卡簇没有跨越整个宽度,则它会居中。

因此,如果您必须在适合纵向模式时将选项卡嵌入到操作栏中,那么您似乎必须使目标 API 版本低于 JB(这意味着应用程序不能使用任何 JB 或更高版本的 API)或处理无论选项卡所需的宽度如何,堆叠布局都是任何设备上纵向模式下的唯一选项。

于 2013-08-06T06:43:05.763 回答
0

现在有一种相当简单(好吧,不那么骇人听闻)的方式。您需要使用 appcompat-v7 和设计库(版本 22.0+)。我假设您使用 aViewPager作为选项卡内容(如果没有,只需ViewPager在下面的代码中省略 -related 内容,并用您自己的选项卡切换逻辑替换它)。让您的活动扩展AppCompatActivity(它具有内置的支持ActionBarViewPager功能),然后在其onCreate()方法中执行以下操作:

    ActionBar actionBar = getSupportActionBar();

    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    Context ctx = new ContextThemeWrapper(getApplication(), R.style.AppTheme);
    mTabLayout = new TabLayout(ctx);
    LinearLayout.LayoutParams mTabLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    mTabLayout.setLayoutParams(mTabLayoutParams);

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        TabLayout.Tab newTab = mTabLayout.newTab();
        // Assuming you use icons on tabs (text should work, too)
        newTab.setIcon(mSectionsPagerAdapter.getPageIcon(i));
        mTabLayout.addTab(newTab);
    }

    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setCustomView(mTabLayout);

    // This will keep tabs and the ViewPager in sync
    // (selecting a tab will switch pages, swiping will update tabs)
    mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

这通过实例化 aTabLayout()并将其添加到ActionBar自定义视图中来工作。

据我所知,这将隐藏应用程序图标和标题。如果您决定需要它们,我还没有想出如何显示它们,但我想您总是可以创建 a LinearLayout,将 theTabLayout和其他任何您想要的东西放在上面,并将其作为自定义视图传递。

于 2016-04-13T12:28:36.853 回答