Android TabBar喜欢Iphone问题

时间:2013-04-26 08:42:58

标签: java android xml android-activity tabs

我正在尝试像Iphone一样实现TabBar,并希望在ListView上显示信息,在选择City之后我得到另一个包含与该城市相关的内容的列表,它在新视图中打开但是当我调用新视图时它会丢失TabBAr并且只显示视图,我需要知道的是他们的任何方式**要显示下一个视图的信息,但无论什么活动正在工作,TABBAR应该保持在底部**

我的第一个标签(主页标签)的屏幕截图

我在哪里选择城市移动到下一个屏幕,TabBar停留在Bottom,然后到下一个View再到下一个,依此类推,直到最后一个屏幕截图我已经显示了一张图片。

但我不知道我应该如何在Android中使用它。

我应如何在我的主页选项卡中的所有视图上显示TabBar

到目前为止,我有这段代码:

TabView.java

TabHostProvider.java

import android.app.Activity;

  public abstract class TabHostProvider {
public Activity context;

public TabHostProvider(Activity context){
    this.context = context;
}

public abstract TabView getTabHost(String category);
 }

[Tab.java] [2]

MyTabHostProvider.java

import android.app.Activity;
import android.content.Intent;
 import android.graphics.Color;
 import android.graphics.drawable.GradientDrawable;

  public class MyTabHostProvider extends TabHostProvider {

private Tab cityTab;
private Tab searchTab;
private Tab mapTab;
private Tab favouriteTab;
private Tab settingTab;

private TabView tabView;
private GradientDrawable gradientDrawable, transGradientDrawable;

public MyTabHostProvider(Activity context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
public TabView getTabHost(String category) {
    // TODO Auto-generated method stub
    tabView = new TabView(context);
    tabView.setOrientation(TabView.Orientation.BOTTOM);
  tabView.setBackgroundID(R.drawable.tab_background_gradient);

    gradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] {0xFFB2DA1D, 0xFF85A315});
    gradientDrawable.setCornerRadius(0f);
    gradientDrawable.setDither(true);

    transGradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] {0x00000000, 0x00000000});
    transGradientDrawable.setCornerRadius(0f);
    transGradientDrawable.setDither(true);

    cityTab = new Tab(context, category);
    cityTab.setIcon(R.drawable.navbar_allnormal);
    cityTab.setIconSelected(R.drawable.navbar_allselected);
    cityTab.setBtnText("City");
    cityTab.setBtnTextColor(Color.WHITE);
    cityTab.setSelectedBtnTextColor(Color.BLACK);

    cityTab.setBtnGradient(transGradientDrawable);
    cityTab.setSelectedBtnGradient(gradientDrawable);
    cityTab.setIntent(new Intent(context, CityActivity.class));

    searchTab = new Tab(context, category);
    searchTab.setIcon(R.drawable.navbar_picturenormal);
    searchTab.setIconSelected(R.drawable.navbar_pictureselected);
    searchTab.setBtnText("Search");
    searchTab.setBtnTextColor(Color.WHITE);
    searchTab.setSelectedBtnTextColor(Color.BLACK);

    searchTab.setBtnGradient(transGradientDrawable);
    searchTab.setSelectedBtnGradient(gradientDrawable);
    searchTab.setIntent(new Intent(context, SearchActivity.class));

    mapTab = new Tab(context, category);
    mapTab.setIcon(R.drawable.navbar_filenormal);
    mapTab.setIconSelected(R.drawable.navbar_fileselected);
    mapTab.setBtnText("Map");
    mapTab.setBtnTextColor(Color.WHITE);
    mapTab.setSelectedBtnTextColor(Color.BLACK);

    mapTab.setBtnGradient(transGradientDrawable);
    mapTab.setSelectedBtnGradient(gradientDrawable);
    mapTab.setIntent(new Intent(context, MapFragment.class));

    favouriteTab = new Tab(context, category);
    favouriteTab.setIcon(R.drawable.navbar_videonormal);
    favouriteTab.setIconSelected(R.drawable.navbar_videoselected);
    favouriteTab.setBtnText("Favourites");
    favouriteTab.setBtnTextColor(Color.WHITE);
    favouriteTab.setSelectedBtnTextColor(Color.BLACK);

    favouriteTab.setBtnGradient(transGradientDrawable);
    favouriteTab.setSelectedBtnGradient(gradientDrawable);
    favouriteTab.setIntent(new Intent(context, FavouritesActivity.class));

    settingTab = new Tab(context, category);
    settingTab.setIcon(R.drawable.navbar_morenormal);
    settingTab.setIconSelected(R.drawable.navbar_moreselected);
    settingTab.setBtnText("Settings");
    settingTab.setBtnTextColor(Color.WHITE);
    settingTab.setSelectedBtnTextColor(Color.BLACK);

    settingTab.setBtnGradient(transGradientDrawable);
    settingTab.setSelectedBtnGradient(gradientDrawable);
    settingTab.setIntent(new Intent(context, SettingsActivity.class));

    tabView.addTab(cityTab);
    tabView.addTab(searchTab);
    tabView.addTab(mapTab);
    tabView.addTab(favouriteTab);
    tabView.addTab(settingTab);

    return tabView;
}

    }

CityActivity.java

public class CityActivity extends Activity {

private final String TAG = CityActivity.class.getSimpleName();
private ListView moreList = null;
private final String[] moreItems = { "Srinagar", "Gulmarg", "Pehlgam", "Jammu", "Leh",
        "Patnitop"};

private TextView tv;
private Button action_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TabHostProvider tabProvider = new MyTabHostProvider(CityActivity.this);
    TabView tabView = tabProvider.getTabHost("City");
    tabView.setCurrentView(R.layout.city);
    setContentView(tabView.render(0));

    moreList = (ListView) findViewById(R.id.moreactivity_list);
    moreList.setAdapter(new MoreCityAdapter(CityActivity.this, moreItems));
    moreList.setOnItemClickListener(new OnItemClickListener() 
    {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            final String activity = moreList.getAdapter().getItem(position).toString().trim();
            Log.d(TAG, "Clicked Item: " + activity);
            tv = (TextView)findViewById(R.id.tv_header_title);
            tv.setText(activity);
              Intent intent = new Intent(CityActivity.this, Second.class);
                startActivity(intent);


    }
});
 }

Second.java

  public class Second extends Activity{

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

        Here i need to show the Tab bar with my List View
       How should i get the Tab Bar here and on my following Third,Fourth & Fifth Activity

}
   }

[MoreCityAdapter.java] [3]

Select City

Select Place

Full Pic

6 个答案:

答案 0 :(得分:3)

请尝试实施this项目/链接 在你的代码中。我认为它对您有用..您只需要将Activity Group实现到您的项目/代码中,它就可以按照您的需要使用标签。:)

答案 1 :(得分:1)

我建议你在Android应用程序中集成这种交互的几种方法,我能想到的。

  1. 首先,您可以在ActionBar中使用LIST_NAVIGATION,您可以在其中添加所有“标签”,并根据用户选择添加/删除Fragments。如果您想支持2.2+以上的旧Android版本,可以使用ActionBarSherlock,这是旧版本的ActionBar API的非常好的实现。您可以在适用于Android的Gmail应用中找到ActionBar中的列表导航示例,并在此处举例说明如何实现此目的:ActionBarSherlock

  2. 您可以使用第二种方式来实现类似这样的方式,就像在Google+,Youtube,Facebook应用中使用SlidingDrawer一样,在我看来这也是一个不错的选择。这个SlidingMenu有一个很棒的图书馆。

  3. 这取决于您认为实现您想要的东西的最佳方式。最后一件事,如果你真的想使用像IOS这样的标签我会建议你做这样的事情:

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost
        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">
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"/>
    
            <FrameLayout
                android:id="@+android:id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
    
            <TabWidget
                android:id="@android:id/tabs"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"/>
    
        </LinearLayout>
    </TabHost>
    

    MainTabActivity

    public class MainActivity extends SherlockFragmentActivity {
    
    private TabHost mTabHost;
    private TabManager mTabManager;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
    
        mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);
    
    
        TabHost.TabSpec spec = mTabHost.newTabSpec("collections");
        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, mTabHost.getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title); // tab title
        title.setText(getString(R.string.collection_tab));
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);  // tab icon
        icon.setImageResource(R.drawable.ic_tab_collections);
        spec.setIndicator(tabIndicator);
        mTabManager.addTab(spec, MyCollectionList.class, null); // WHERE MyCollectionList is Fragment attached to first tab.
    
        // MYLIST TAB
        TabHost.TabSpec spec5 = mTabHost.newTabSpec("mylist");
        View tabIndicator5 = LayoutInflater.from(this).inflate(R.layout.tab_layout, mTabHost.getTabWidget(), false); // used for custom design of tab
        TextView title5 = (TextView) tabIndicator5.findViewById(R.id.title); // tab title
        title5.setText(getString(R.string.my_list));
        ImageView icon5 = (ImageView) tabIndicator5.findViewById(R.id.icon); // tab icon
        icon5.setImageResource(R.drawable.ic_tab_mylist);
        spec5.setIndicator(tabIndicator5);
        mTabManager.addTab(spec5, MyList.class, null); // MyList - second Fragment attached to second tab
    
        // ADD as many tabs as you want
    
    
        mTabHost.setCurrentTab(0); // set selected tab
    
        }
    
    }
    

    TabManager.class (它取自ActionBarSherlock示例,稍微修改一下版本):

    import java.util.HashMap;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.View;
    import android.widget.TabHost;
    
    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost.  It relies on a
     * trick.  Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show.  This is not sufficient for switching
     * between pages.  So instead we make the content part of the tab host
     * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
     * view to show as the tab content.  It listens to changes in tabs, and takes
     * care of switch to the correct paged in the ViewPager whenever the selected
     * tab changes.
     */
    public class TabManager implements TabHost.OnTabChangeListener {
        private final FragmentActivity mActivity;
        private final TabHost mTabHost;
        private final int mContainerId;
        private int mLastFragmentId;
        private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
        TabInfo mLastTab;
    
        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;
            private Fragment fragment;
    
            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }
    
        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;
    
            public DummyTabFactory(Context context) {
                mContext = context;
            }
    
            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }
    
        public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
            mActivity = activity;
            mTabHost = tabHost;
            mContainerId = containerId;
            mTabHost.setOnTabChangedListener(this);
        }
    
        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mActivity));
            String tag = tabSpec.getTag();
    
            TabInfo info = new TabInfo(tag, clss, args);
    
            // Check to see if we already have a fragment for this tab, probably
            // from a previously saved state.  If so, deactivate it, because our
            // initial state is that a tab isn't shown.
            info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
            if (info.fragment != null && !info.fragment.isDetached()) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
                ft.detach(info.fragment);
                ft.commit();
            }
    
            mTabs.put(tag, info);
            mTabHost.addTab(tabSpec);
        }
    
        @Override
        public void onTabChanged(String tabId) {
            TabInfo newTab = mTabs.get(tabId);
            if (mLastTab != newTab) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
                if (mLastTab != null) {
                    FragmentManager mManager = mActivity.getSupportFragmentManager();
                    if(mManager.getBackStackEntryCount() > 0){
                        mLastFragmentId = mManager.getBackStackEntryAt(0).getId();
                        mManager.popBackStack(mLastFragmentId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    }
                    if (mLastTab.fragment != null) {
                        ft.detach(mLastTab.fragment);
                    }
                }
                if (newTab != null) {
                    if (newTab.fragment == null) {
                        newTab.fragment = Fragment.instantiate(mActivity,
                                newTab.clss.getName(), newTab.args);
                        ft.add(mContainerId, newTab.fragment, newTab.tag);
                    } else {
                        ft.attach(newTab.fragment);
                    }
                }
    
                mLastTab = newTab;
                ft.commit();
                mActivity.getSupportFragmentManager().executePendingTransactions();
            }
            }
        }
    

    希望这篇文章可以帮助您找到实现您想要的最佳方式! :)

答案 2 :(得分:0)

我建议你使用专为Android Design Pattern设计的ActionBar。 - 不推荐使用TabBarActivity     http://developer.android.com/reference/android/app/TabActivity.html

更多信息:  http://www.androiduipatterns.com/2011/12/how-should-android-apps-look-like_16.html

答案 3 :(得分:0)

您需要哪个类,该类名称赋予该选项卡意图..

   Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
   // Intent intent;  // Reusable Intent for each tab
    // Create an Intent to launch an Activity for the tab (to be reused)
    Intent intent1 = new Intent().setClass(this, Second.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("app_name").setIndicator("Places",
                  .setContent(intent1);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(0);
    // Do the same for the other tabs
    Intent intent2 = new Intent().setClass(this, Third.class);
    spec = tabHost.newTabSpec("application").setIndicator("City",
                  .setContent(intent2);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(1);
    Intent intent3 = new Intent().setClass(this, First.class);
    spec = tabHost.newTabSpec("toplinks").setIndicator("VISTED",
                  .setContent(intent3);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(2);
    tabHost.setCurrentTab(0);
}

Tab.xml

       <?xml version="1.0" encoding="utf-8"?>
    <TabHost 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

    <LinearLayout 
android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical">
   <TabWidget 
android:id="@android:id/tabs" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></TabWidget>
    <FrameLayout 
android:id="@android:id/tabcontent" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"></FrameLayout>
    </LinearLayout>
    </TabHost>

让我解决问题......

答案 4 :(得分:0)

就像已经消化过的那样,我也建议使用动作栏,不仅因为TabBarActivity已被弃用,而且因为它是Android的UI设计。 一个Android用户想要一个类似UI而不是iOS的用户,因为他或她不习惯这种工作流程。

如果您想要legecy平台支持,我会推荐这个库。 http://actionbarsherlock.com/ 许多应用都使用此功能,包括whatsapp foursquare等等。

对于你的porpuse,你正在寻找寻呼机,进行谷歌搜索,你会发现很多信息与动作栏sherlock组合。 或者查看Actionbar提供的示例,他们几乎都有样本中的所有场景。

希望这有帮助。

答案 5 :(得分:0)

是的,你可以这样做

我这样做是通过为TabHost上的每个选项卡创建一个堆栈,堆栈包含所有视图,我使用了这样的ActivityGroup。

1 - 创建堆栈管理器

import java.util.Stack;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

@SuppressWarnings("deprecation")
public class FirstStackManager extends ActivityGroup {

        private Stack<String> MY_STACK;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (MY_STACK == null)
            MY_STACK = new Stack<String>();
        // start default activity
        push("FirstActivity", new Intent(this, FirstActivity.class));
    }

    @Override
    public void finishFromChild(Activity child) {
        pop();
    }

    @Override
    public void onBackPressed() {
        pop();
    }

    public void push(String id, Intent intent) {
        Window window = getLocalActivityManager().startActivity(id,
                intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK));
        if (window != null) {
                MY_STACK.push(id);
            setContentView(window.getDecorView());
        }
    }

    public void pop() {
        if (MY_STACK.size() == 1)
            finish();
        LocalActivityManager manager = getLocalActivityManager();
        manager.destroyActivity(MY_STACK.pop(), true);
        if (MY_STACK.size() > 0) {
            Intent lastIntent = manager.getActivity(MY_STACK.peek())
                    .getIntent();
            Window newWindow = manager.startActivity(
                    MY_STACK.peek(), lastIntent);
            setContentView(newWindow.getDecorView());
        }
    }
}
  

不要忘记在Manifest文件中添加FirstStackManager

2 - 在您的主要活动中使用类似的堆栈

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

        TabHost tabHost;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.tab);


            tabHost = getTabHost();
        setTabs();
    }

    private void setTabs() {
        addTab("FirstTab", R.drawable.tab_first, FirstStackManager.class);
        addTab("SecondTab", R.drawable.tab_second, SecondStackManager.class);
                /*Other tab*/
    }

    private void addTab(String labelId, int drawableId, Class<?> c) {
        Intent intent = new Intent().setClass(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

        View tabIndicator = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, getTabWidget(), false);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);
        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

您可以在此处找到如何创建xmls文件Raised-Center-Tab-in-Android

3 - 在您的FirstActivity上推送像这样的新视图

public class FirstActivity extends Activity{

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

      /*push a view on list click listeneer*/

       Intent intent = new Intent();
       intent.setClass(FirstActivity.this, DetailActivity.class);
       FirstStackManager activityStack = (FirstStackManager) getParent();
       activityStack.push("DetailActivity", intent); 

}
   }

4 - 在您的DetailActivity流行视图中

public class DetailActivity extends Activity{

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

      /*pop a view on back button click listeneer*/

       FirstStackManager activityStack = (FirstStackManager) getParent();
       activityStack.pop(); 

}
   }