【问题标题】:Why wont my regular Tabhost work w. ActionBarSherlock?为什么我的常规 Tabhost 不能使用。动作酒吧夏洛克?
【发布时间】:2013-01-28 04:30:17
【问题描述】:

我正在努力让一个简单的 tabhost 与 ABS 一起工作,似乎 Android 在尝试“改进”它们时只会让事情变得更加复杂。我正在考虑将我的项目转换为在标签中使用片段,但事实证明,对于我在每个标签中使用的复杂布局来说,这是一种方式。我现在只需要一个普通的 ol 工作 tabhost w。工作腹肌吧。

package com.abs.tabs.fragments;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;

import com.actionbarsherlock.view.MenuItem;
import com.buhz.feeds.Buhdz;
import com.buhz.login.R;
import com.buhzhyve.localz.Localz;
import com.buhzhyve.trails.Trails;
import com.buhzhyve.trendz.Trendz;

public class FragmentTabs extends SherlockActivity {
    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         setTheme(SampleList.THEME); //Used for theme switching in samples
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);


        actionBar=getSupportActionBar();

         Resources res = getResources(); // Resource object to get Drawables

            TabHost tabHost =(TabHost) findViewById(android.R.id.tabhost);  // The activity TabHost

            TabHost.TabSpec spec;  // Resusable TabSpec for each tab

            Intent intent;  // Reusable Intent for each tab

            // Do the same for the other tabs

            intent = new Intent().setClass(this, Buhdz.class);

            spec = tabHost.newTabSpec("Buhdz").setIndicator("Feeds",

                              res.getDrawable(R.drawable.buhdz_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);

         // Do the same for the other tabs

            intent = new Intent().setClass(this, Localz.class);

            spec = tabHost.newTabSpec("Localz").setIndicator("Locals",

                              res.getDrawable(R.drawable.locals_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);



            intent = new Intent().setClass(this, Trendz.class);

            spec = tabHost.newTabSpec("Trendz").setIndicator("Trends",

                              res.getDrawable(R.drawable.trends_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);



            intent = new Intent().setClass(this, Localz.class);

            spec = tabHost.newTabSpec("convos").setIndicator("Convos",

                              res.getDrawable(R.drawable.convos_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);

         // Create an Intent to launch an Activity for the tab (to be reused)

            intent = new Intent().setClass(this, Trails.class);

            // Initialize a TabSpec for each tab and add it to the TabHost

            spec = tabHost.newTabSpec("Trails").setIndicator("Trails",

                              res.getDrawable(R.drawable.trails_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);
            tabHost.setCurrentTab(4);
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.item1:
            return true;
        case R.id.subItem1:
            Toast.makeText(this, "Sub Menu item 1 tapped", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.subItem2:
            Toast.makeText(this, "Sub Menu item 2 tapped", Toast.LENGTH_SHORT).show();
            return true;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 我不明白你的问题,但你知道你不能混合actionbarsherlock和原生actionbar?
  • 我一直想要的只是一个工作的 ABS 操作栏和工作选项卡。如果我能侥幸使用我的旧标签 w。活动 id 很酷(我的标签代码几乎与此相同:androidhive.info/2011/08/android-tab-layout-tutorial)如果我必须学习并为我的标签使用片段,那么我需要帮助将活动转换为片段。

标签: android android-fragments android-activity actionbarsherlock android-tabhost


【解决方案1】:
  1. 使用这个库:https://github.com/JakeWharton/Android-ViewPagerIndicator,您可以在这个库中找到一些带有您想要的选项卡的示例,并与 sherlockactionbar 一起使用。
  2. 访问此问题:Actionbarsherlock + tabs + multi fragments?
  3. 如果遇到麻烦,可以问我。

【讨论】:

  • 我已经让选项卡工作了,但现在我的问题是,如果我使用选项卡 + 使用按钮的片段因为片段不能很好地播放 w,我该如何在我的应用程序中移动。意图?例如,如果我的用户在选项卡 3 上(让我们称之为选项卡配置文件)并且他们单击一个图像按钮,假设将他们从他们的配置文件带到朋友那里我会怎么做? ViewPageIndicator 是使用 tab2 内的按钮从 tab2 切换到 tab3 的普遍接受的默认方式吗?
  • 嗯,有可能,有很多方法可以做到这一点,在片段的布局中,您可以轻松添加按钮/图像按钮并调用意图,也许您仍然不了解片段的工作原理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
相关资源
最近更新 更多