Android标签操作栏

前端之家收集整理的这篇文章主要介绍了Android标签操作栏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试3.0的 android操作栏,我参考

http://www.youtube.com/watch?v=gMu8XhxUBl8

TabsActivity中的代码如下:

package com.test.actionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment; 
import android.app.FragmentTransaction;
import android.os.Bundle;

public class TabsActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tabA = bar.newTab().setText("A Tab");
    ActionBar.Tab tabB = bar.newTab().setText("B Tab");
    ActionBar.Tab tabC = bar.newTab().setText("C Tab");

    Fragment fragmentA = new AFragmentTab();
    Fragment fragmentB = new BFragmentTab();
    Fragment fragmentC = new CFragmentTab();

    tabA.setTabListener(new MyTabsListener(fragmentA));
    tabB.setTabListener(new MyTabsListener(fragmentB));
    tabC.setTabListener(new MyTabsListener(fragmentC));

    bar.addTab(tabA);
    bar.addTab(tabB);
    bar.addTab(tabC);

}

protected class MyTabsListener implements ActionBar.TabListener {

    private Fragment fragment;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabReselected(Tab tab,FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    public void onTabSelected(Tab tab,FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.add(R.id.fragment_container,fragment,null);

    }

    public void onTabUnselected(Tab tab,FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
  }
}

但是,对于一步一步的教程,在完成本教程之后,我意识到在TabsActivity中,在onTabSelected方法中,它将需要一个变量即container_id,我不太确定即使在查看之后我也能提供它在api.我尝试删除该行并在平板电脑上运行它,但它抛出了一个runtimeexception.

谁能帮我这个?

对不起,我是Android编程的新手,如果问题听起来太简单了.

提前致谢.

编辑

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;

public class ActionBarTabs extends Activity {

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tabA = bar.newTab().setText("A Tab");
    ActionBar.Tab tabB = bar.newTab().setText("B Tab");
    ActionBar.Tab tabC = bar.newTab().setText("C Tab");

    bar.addTab(tabA);
    bar.addTab(tabB);
    bar.addTab(tabC);
  }
}

UPDATE

package com.debug.actionbartabs;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;

public class TabsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tabA = bar.newTab().setText("A Tab");
   bar.addTab(tabA);

   }
}

解决方法

每个类应如下所示:
public class AFragmentTab extends Fragment
{
  @Override
  public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
  {
    return inflater.inflate(R.layout.fragment_a,container,false);
  }
}

主要活动应如下所示:

package com.test.actionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment; 
import android.app.FragmentTransaction;
import android.os.Bundle;

public class TabsActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tabA = bar.newTab().setText("A Tab");
    ActionBar.Tab tabB = bar.newTab().setText("B Tab");
    ActionBar.Tab tabC = bar.newTab().setText("C Tab");

    Fragment fragmentA = new AFragmentTab();
    Fragment fragmentB = new BFragmentTab();
    Fragment fragmentC = new CFragmentTab();

    tabA.setTabListener(new MyTabsListener(fragmentA));
    tabB.setTabListener(new MyTabsListener(fragmentB));
    tabC.setTabListener(new MyTabsListener(fragmentC));

    bar.addTab(tabA);
    bar.addTab(tabB);
    bar.addTab(tabC);

}

protected class MyTabsListener implements ActionBar.TabListener {

    private Fragment fragment;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabReselected(Tab tab,FragmentTransaction ft) {
    }

    public void onTabSelected(Tab tab,FragmentTransaction ft) {
        ft.add(R.id.fragment_container,null);
    }

    public void onTabUnselected(Tab tab,FragmentTransaction ft) {
        // some people needed this line as well to make it work: 
        ft.remove(fragment);
    }
}

我刚刚在这里找到了他的代码的副本:http://www.abelski.com/courses/android3ui/actionbar.pdf> _<
所以在main.xml中看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/fragment_container"></LinearLayout>
</LinearLayout>
原文链接:https://www.f2er.com/android/311144.html

猜你在找的Android相关文章