Android的标签操作栏

Android的标签操作栏

本文介绍了Android的标签操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Android操作栏为3.0,在这里我指的是

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

在code。在 TabsActivity 如下:

 包com.test.actionbar;

进口android.app.ActionBar;
进口android.app.ActionBar.Tab;
进口android.app.Activity;
进口android.app.Fragment;
进口android.app.FragmentTransaction;
进口android.os.Bundle;

公共类TabsActivity延伸活动{

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    动作条酒吧= getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab塔巴= bar.newTab()的setText(选项卡)。
    ActionBar.Tab塔布= bar.newTab()的setText(B选项卡)。
    ActionBar.Tab TABC = bar.newTab()的setText(C选项卡)。

    碎片碎裂=新AFragmentTab();
    片段fragmentB =新BFragmentTab();
    片段fragmentC =新CFragmentTab();

    tabA.setTabListener(新MyTabsListener(碎裂));
    tabB.setTabListener(新MyTabsListener(fragmentB));
    tabC.setTabListener(新MyTabsListener(fragmentC));

    bar.addTab(塔巴);
    bar.addTab(塔布);
    bar.addTab(TABC);

}

保护类MyTabsListener实现ActionBar.TabListener {

    私人片段片段;

    公共MyTabsListener(片段片段){
        this.fragment =片段;
    }

    公共无效onTabReselected(TAB键,FragmentTransaction英尺){
        // TODO自动生成方法存根

    }

    公共无效onTabSelected(TAB键,FragmentTransaction英尺){
        // TODO自动生成方法存根
        ft.add(R.id.fragment_container,片段,NULL);

    }

    公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
        // TODO自动生成方法存根

    }
  }
}
 

一步步的教程,但是,在完成本教程后,我意识到,在TabsActivity,在onTabSelected方法,它需要一个变量,它是container_id,我也不太清楚我怎样才能提供该即使在看的API。我试图消除线并运行它的平板电脑,但它引发了我一个RuntimeException。

谁能帮助我?

不好意思,我是新来的Andr​​oid程序,如果这个问题听起来太简单了。

在此先感谢。

修改

 进口android.app.ActionBar;
进口android.app.Activity;
进口android.app.Fragment;
进口android.os.Bundle;

公共类ActionBarTabs延伸活动{

公共无效的onCreate(包savedInstanceState){

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

    动作条酒吧= getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab塔巴= bar.newTab()的setText(选项卡)。
    ActionBar.Tab塔布= bar.newTab()的setText(B选项卡)。
    ActionBar.Tab TABC = bar.newTab()的setText(C选项卡)。

    bar.addTab(塔巴);
    bar.addTab(塔布);
    bar.addTab(TABC);
  }
}
 

更新

 包com.debug.actionbartabs;

进口android.app.ActionBar;
进口android.app.Activity;
进口android.os.Bundle;

公共类TabsActivity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    动作条酒吧= getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab塔巴= bar.newTab()的setText(选项卡)。
   bar.addTab(塔巴);

   }
}
 

解决方案

每个班级应该是这样的:

 公共类AFragmentTab扩展片段
{
  @覆盖
  公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
  {
    返回inflater.inflate(R.layout.fragment_a,集装箱,假);
  }
}
 

和主要活动应该是这样的:

 包com.test.actionbar;

进口android.app.ActionBar;
进口android.app.ActionBar.Tab;
进口android.app.Activity;
进口android.app.Fragment;
进口android.app.FragmentTransaction;
进口android.os.Bundle;

公共类TabsActivity延伸活动{

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    动作条酒吧= getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab塔巴= bar.newTab()的setText(选项卡)。
    ActionBar.Tab塔布= bar.newTab()的setText(B选项卡)。
    ActionBar.Tab TABC = bar.newTab()的setText(C选项卡)。

    碎片碎裂=新AFragmentTab();
    片段fragmentB =新BFragmentTab();
    片段fragmentC =新CFragmentTab();

    tabA.setTabListener(新MyTabsListener(碎裂));
    tabB.setTabListener(新MyTabsListener(fragmentB));
    tabC.setTabListener(新MyTabsListener(fragmentC));

    bar.addTab(塔巴);
    bar.addTab(塔布);
    bar.addTab(TABC);

}

保护类MyTabsListener实现ActionBar.TabListener {

    私人片段片段;

    公共MyTabsListener(片段片段){
        this.fragment =片段;
    }

    公共无效onTabReselected(TAB键,FragmentTransaction英尺){
    }

    公共无效onTabSelected(TAB键,FragmentTransaction英尺){
        ft.add(R.id.fragment_container,片段,NULL);
    }

    公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
        //有些人需要这条线也使其工作:
        ft.remove(片段);
    }
}
 

我刚刚找到了他的code复制在这里: HTTP:// WWW .abelski.com /场/ android3ui / actionbar.pdf > _<因此,在main.xml中看起来是这样的:

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
< LinearLayout中的android:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent
机器人:ID =@ + ID / fragment_container>< / LinearLayout中>
< / LinearLayout中>
 

I am trying out on the android action bar for 3.0, where I refer to

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

The code in the TabsActivity are as follow:

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

    }
  }
}

for a step by step tutorial, however, after completing the tutorial, I realised that in the TabsActivity, in the onTabSelected method, it will require a variable which is the container_id, which i am not too sure how can i supply that even after looking at the api. I tried removing the line and ran it on the tablet but it throws me a runtimeexception.

Can anyone help me with this?

sorry, I am new to android programming, if the question sounds too simple.

Thanks in advance.

EDIT

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);

   }
}
解决方案

Each of the classes should look like this:

public class AFragmentTab extends Fragment
{
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    return inflater.inflate(R.layout.fragment_a, container, false);
  }
}

And the main activity should look like this:

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, fragment, null);
    }

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

I just found the copy of his code here: http://www.abelski.com/courses/android3ui/actionbar.pdf >_<So in the main.xml looks like this:

<?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>

这篇关于Android的标签操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:48