【问题标题】:ActionBar Sherlock , action bar tabs and fragmentsActionBar Sherlock ,操作栏选项卡和片段
【发布时间】:2012-06-06 09:55:34
【问题描述】:

我想为手机和平板电脑创建一个应用程序。我正在使用 actiobBar sherlock 库。我有一个扩展 SherlockFragmentActivity 的 MainActivity。 MainActivity 有一个仅包含选项卡的操作栏。 现在,我想为每个选项卡关联一个片段,这个片段应该有两个片段(列表片段和详细片段)。请建议我如何做到这一点。

【问题讨论】:

  • 你有没有发现如何做到这一点?

标签: android tabs fragment actionbarsherlock


【解决方案1】:

有几种方法,我只解释我的。

您可以使用任何导航、ViewPager、TabAdapter... 这个想法是添加一层片段

例如,您的 TabAdapter 带有视图寻呼机:

    private ViewPager           _viewPager;
    private TabsAdapter         _tabsAdapter;

   _tabsAdapter = new TabsAdapter(this, _viewPager);
   _tabsAdapter.addTab(bar.newTab().setCustomView(getTabIndicator(getString(R.string.tle_tab_home), R.drawable.ic_menu_home)), FragmentAccueil.class, null);
   _tabsAdapter.addTab(bar.newTab().setCustomView(getTabIndicator(getString(R.string.tle_tab_directory), R.drawable.tab_list_icon)), ContainerFragmentAnnuaire.class, null);

ContainerFragmentAnnuaire 是一个包含片段的布局

例如对于手机,您将创建此布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment class="com.example.android.ContainerFragmentAnnuaire"
              android:id="@+id/list_frag"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
</FrameLayout>

对于平板电脑,您将创建这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/frags">
  <fragment class="com.example.android.ContainerFragmentAnnuaire"
            android:id="@+id/list_frag"
            android:layout_width="@dimen/titles_size"
            android:layout_height="match_parent"/>
  <fragment class="com.example.android.ContainerFragmentDetail"
            android:id="@+id/details_frag"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

只需在好文件夹中添加布局

res/layout/layout.xml 用于手机

res/layout-large/layout.xml 用于平板电脑

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    相关资源
    最近更新 更多