2015-04-27 66 views
0

我想使用ActionBar而不擴展ActionBarActivity,因爲我已經擴展了TabActivity。 任何人都可以請幫忙嗎?android的ActionBar沒有擴展ActionBarActivity?

我已經做了這樣的事情。在此我要實施Action Bar

public class MainActivity extends TabActivity { 

private TabHost myTabHost; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    myTabHost=getTabHost(); 
+4

'TabActivity'已被棄用**超過四年**。請使用現代技術製表符,例如帶有標籤指示符的'ViewPager'。 – CommonsWare

回答

0

試試我的代碼。您將能夠使用操作欄和tabhost分段。

1) XML file for Activity: 

    <?xml version="1.0" encoding="utf-8"?> 
<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:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <HorizontalScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:fillViewport="true" 
      android:scrollbars="none"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0"/> 
     </HorizontalScrollView> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0"/> 

     <FrameLayout 
      android:id="@+id/realtabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 

    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 


2) Java code for Activity: 

    public class CarsActivity extends FragmentActivity implements YourFragment.OnFragmentInteractionListener 
    { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    _mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); 
    _mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 
    //add your fragments here: 
    _mTabHost.addTab(_mTabHost.newTabSpec("*UNIQE IDENTIFIER OF YOUR FRAGMENT").setIndicator("*DISPLAY NAME ON TAB*"), 
       YourFragment.class, null); 
    } 
    }); 
    @Override 
    public void onFragmentInteraction(Uri uri) 
     {} 

    } 
0

可以在oncreate()使用getActionBar()或者,如果你想創建自定義的操作欄,然後用下面的代碼。

// Action Bar Customization 
    ActionBar ab =act.getActionBar(); 

    ColorDrawable colorDrawable = new ColorDrawable(act.getResources().getColor(color.ActionBar_bg)); 
    ab.setBackgroundDrawable(colorDrawable); 

    //ab.setBackgroundDrawable(act.getResources().getDrawable(R.drawable.header)); 
    ab.setDisplayShowTitleEnabled(false); // disables default title on 
              // actionbar. 
    ab.setDisplayShowCustomEnabled(true); // enables custom view. 
    ab.setDisplayShowHomeEnabled(false); // hides app icon. 
    ab.setTitle(""); 
    // Inflating Layout 
    LayoutInflater inflater = (LayoutInflater) act.getActionBar() 
      .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE); 

    View customActionBar = inflater.inflate(R.layout.actionbar_layout, null); 

    imgapplogo = (ImageView) customActionBar.findViewById(R.id.app_logo); 
    img_back=(ImageView)customActionBar.findViewById(R.id.imgBack); 
相關問題