0

我已经看到包含面板的应用程序带有不会将用户引导到其他活动的选项卡。我想,这就是 TabHost + Fragment 元素被使用的地方。

您能否就实现此功能提供您的假设/警告/链接?

4

3 回答 3

2

使用时可以利用 ViewPager。

首先在你的布局中包含 ViewPager(你可以使用旧版本的兼容性包中的那个):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="0dp"
        android:layout_weight="1" />

</RelativeLayout>

要将多个 Fragment 加载到 ViewPager 中,您必须实现 FragmentPagerAdapter:

public class MyPagerAdapter extends FragmentPagerAdapter {
    private Fragment fragment1;
    private Fragment fragment2;

    public ChartPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        fragment1 = new MyFragment();
        fragment2 = new MyFragment();
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return fragment1;
            case 1:
                return fragment2;

        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public String getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Title 1";
            case 1:
                return "Title 2";
        }
    }
}

然后将此适配器设置为 ViewPager:

    setContentView(R.layout.my_layout);

    MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), MyActivity.this);

    ViewPager pager = (ViewPager)findViewById(R.id.pager);
    pager.setAdapter(adapter);
于 2012-07-19T09:27:21.800 回答
2

使用的好处TabHost是你可以把它放在任何地方。它可以作为一个View本身来实现。

这是我发现的两个两个很好的链接:

带有片段的 Android 选项卡

博客文章

于 2012-07-19T09:28:29.223 回答
0

您可以扩展TabActivity,如下所示:

public class NavigationBarActivity extends TabActivity (
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTabs();
    }

    private void setTabs(){
        TabHost tabHost = getTabHost(); // The activity TabHost    
        TabHost.TabSpec spec;           // Reusable TabSpec for each tab    
        Intent intent;                  // Reusable Intent for each tab

        LayoutInflater inflater = getLayoutInflater();

        // tab 1------------------------------
        View tabView = inflater.inflate(R.layout.tab1, null, false);
        ImageView tabImage = (ImageView)tabView.findViewById(R.id.tabImage);
         TextView tabText = (TextView)tabView.findViewById(R.id.tabText);

        // set icon
        tabImage.setImageResource(R.drawable.tab1);

        // set tab text
        tabText.setText(R.string.tab1);

        // Create an Intent to launch an Activity for the tab (to be reused)    
        intent = new Intent().setClass(this, Activity1.class);    
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("ta1");
        spec.setIndicator(tabView);
        spec.setContent(intent);
        tabHost.addTab(spec);

        // tab 2 -----------------------------------------
        tabView = inflater.inflate(R.layout.tab2, null, false);
        tabImage = (ImageView)tabView.findViewById(R.id.tabImage);
        tabText = (TextView)tabView.findViewById(R.id.tabText);

        // set icon
        tabImage.setImageResource(R.drawable.tab2); 

        // set tab text
        tabText.setText(R.string.tab2);

        intent = new Intent().setClass(this, ChatListActivity.class);    
        spec = tabHost.newTabSpec("tab2");
        spec.setIndicator(tabView);                  
        spec.setContent(intent);    
        tabHost.addTab(spec);

        tabHost.setOnTabChangedListener(this);
    }

    @Override
    public void onTabChanged(String tabId) {
        if(getTabHost().getCurrentTab() == 0) {
            // WE are on tab1 ...
        }       
    }
}
于 2012-07-19T09:37:37.967 回答