代码之家  ›  专栏  ›  技术社区  ›  Vinny K

使用ActionBarSherlock跨片段使用一个通用按钮

  •  1
  • Vinny K  · 技术社区  · 14 年前

    我使用ActionBarSherlock在我的应用程序上获得了一些Holo主题的选项卡和ActionBar,并创建了一个Fragment来处理每个选项卡上的行为。

    在我的“活动”中,我创建了这样的选项卡。

    public class InviteFriendsActivity extends SherlockFragmentActivity implements ActionBar.TabListener
    {
      protected void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
    
        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
        ActionBar.Tab tab1 = bar.newTab();
        tab1.setText("Tab 1");
        tab1.setTabListener(this);  
    
        ActionBar.Tab tab2 = bar.newTab();
        tab2.setText("Tab 2");
        tab2.setTabListener(this);  
    
        bar.addTab(tab1);
        bar.addTab(tab2);
      }
    }
    

    然后我创建了onTabSelected

     public void onTabSelected(Tab tab, FragmentTransaction ft)
    {
        if (tab.getPosition() == 0)
        {
            Fragment1 frag = new Fragment1();
            ft.replace(android.R.id.content, frag);
        }
        else if (tab.getPosition() == 1)
        {
            Fragment2 frag = new Fragment2();
            ft.replace(android.R.id.content, frag);
        }
    }
    

    我可以显示或更改选项卡,但我似乎不知道如何获得一个按钮,该按钮位于屏幕底部,在该活动中保持静止,并且仍然允许我在两个片段之间切换。

    1 回复  |  直到 14 年前
        1
  •  2
  •   Leandros    14 年前

    你想要一个通过每个片段和每个选项卡显示的按钮吗?
    这可以通过使用显示碎片的碎片容器轻松完成。 例如,使用这样的布局:

    <?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" >
    
        <fragment
            android:name="com.example.yourfragmentcontainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button1"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:text="Button" />
    
    </RelativeLayout>
    

    有关如何使用碎片容器设置ActonBar的帮助,请参阅本教程: http://arvid-g.de/12/android-4-actionbar-with-tabs-example