2012-10-16 195 views
4

我試圖在我的應用程序的底部放置操作欄選項卡,但我需要一些幫助(如果它甚至可能)。底部的操作欄選項卡?

代碼:

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     ActionBar.Tab tabA = actionBar.newTab().setText("Games"); 
     ActionBar.Tab tabB = actionBar.newTab().setText("Apps"); 
     ActionBar.Tab tabC = actionBar.newTab().setText("Themes"); 

     tabA.setTabListener(new ActionBarTabListener()); 
     tabB.setTabListener(new ActionBarTabListener()); 
     tabC.setTabListener(new ActionBarTabListener()); 

     actionBar.addTab(tabA); 
     actionBar.addTab(tabB); 
     actionBar.addTab(tabC); 
+0

您是否找到解決方案? – powder366

+0

@ powder366這可以很容易地使用ViewPager和TabPagerIndicator完成 – starkej2

回答

1

它種違背了Android的指引把標籤上的底部,但split action bar應該能夠完成你想要做什麼。

+0

我已經試過了,但標籤仍然在頂部......唯一移動到底部的是MenuItem。 –

+0

您可能只需實現自定義選項卡或使用ViewPager和ViewPagerIndicator,我不確定股票操作欄是否有底部的選項卡 – starkej2

0

還有另一種方法,但你必須使用FrgamentTabHost。

這是代碼。 佈局/ activity_main.xml中

enter <android.support.v4.app.FragmentTabHost 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" >  

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
     <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom"/> 
</LinearLayout> 

你必須把TabWidget在下面的FrameLayout。

佈局/ fragment_layout.xml

enter code here<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center_horizontal" 
android:orientation="vertical" 
android:background="#eaecee"> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical|center_horizontal" 
    android:text="@string/hello_world" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

MainActivity.java

import android.os.Bundle; 
 
import android.support.v4.app.FragmentActivity; 
 
import android.support.v4.app.FragmentTabHost; 
 

 
public class MainActivity extends FragmentActivity { 
 
    private FragmentTabHost mTabHost; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 

 
     setContentView(R.layout.activity_main); 
 
     mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
 
     mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 
 

 
     mTabHost.addTab(
 
       mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null), 
 
       FragmentTab.class, null); 
 
     mTabHost.addTab(
 
       mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null), 
 
       FragmentTab.class, null); 
 
     mTabHost.addTab(
 
       mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null), 
 
       FragmentTab.class, null); 
 
    } 
 
}

FragmentTab.java

import android.os.Bundle; 
 
import android.support.v4.app.Fragment; 
 
import android.view.LayoutInflater; 
 
import android.view.View; 
 
import android.view.ViewGroup; 
 
import android.widget.TextView; 
 

 
public class FragmentTab extends Fragment { 
 

 
    @Override 
 
    public void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
    } 
 

 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
 
          Bundle savedInstanceState) { 
 
     View v = inflater.inflate(R.layout.fragment_layout, container, false); 
 
     TextView tv = (TextView) v.findViewById(R.id.text); 
 
     tv.setText(this.getTag() + " Content"); 
 
     return v; 
 
    } 
 
}

我希望這會幫助你。