带TabHost的导航抽屉

时间:2014-11-18 12:18:01

标签: android android-layout android-tabhost

我想在我的应用程序中实现导航抽屉,而不是列表视图,当用户点击应用程序图标时,将显示标签主机。下面是我的代码 -

<android.support.v4.widget.DrawerLayout      xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- The main content view -->

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="320dp"
    android:layout_height="fill_parent" >

    <LinearLayout
        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" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</TabHost>

以下是我的TabActivity -

public class TabBar extends TabActivity implements OnTabChangeListener {

/** Called when the activity is first created. */
TabHost tabHost;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_bar);
    // ActionBar actionBar = getActionBar();
    // actionBar.setDisplayShowTitleEnabled(false);
    // actionBar.setIcon(android.R.color.transparent);
    // getActionBar().setDisplayHomeAsUpEnabled(true);
    // getActionBar().setHomeButtonEnabled(true);
    mTitle = mDrawerTitle = getTitle();
    // Initiate navigation drawer object
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // Get TabHost Refference
    tabHost = getTabHost();
    // Set TabChangeListener called when tab changed
    tabHost.setOnTabChangedListener(this);

    TabHost.TabSpec spec;
    Intent intent;

    /************* TAB1 ************/
    // Create Intents to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Tab1.class);
    spec = tabHost.newTabSpec("First").setIndicator("").setContent(intent);

    // Add intent to tab
    tabHost.addTab(spec);

    /************* TAB2 ************/
    intent = new Intent().setClass(this, Tab2.class);
    spec = tabHost.newTabSpec("Second").setIndicator("").setContent(intent);
    tabHost.addTab(spec);

    /************* TAB3 ************/
    intent = new Intent().setClass(this, Tab3.class);
    spec = tabHost.newTabSpec("Third").setIndicator("").setContent(intent);
    tabHost.addTab(spec);

    // Set drawable images to tab
    tabHost.getTabWidget().getChildAt(1)
            .setBackgroundResource(R.drawable.tab2);
    tabHost.getTabWidget().getChildAt(2)
            .setBackgroundResource(R.drawable.tab3);

    // Set Tab1 as Default tab and change image
    tabHost.getTabWidget().setCurrentTab(0);
    tabHost.getTabWidget().getChildAt(0)
            .setBackgroundResource(R.drawable.tab1_over);

    mDrawerToggle = new ActionBarDrawerToggle(TabBar.this, mDrawerLayout,
            R.drawable.ic_launcher, R.string.app_name, R.string.app_name) {
        public void onDrawerClosed(View view) {
            // getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            // invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            // getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            // invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }

    // switch (item.getItemId()) {
    // case R.id.action_home:
    // inflateHomeLayout(MODE_NORMAL);
    // return true;
    // case R.id.action_favourite:
    // inflateFavouriteLayout(MODE_NORMAL);
    // return true;
    // case R.id.action_search:
    // Intent intent = new Intent(MidnightMainActivity.this,
    // SearchActivity.class);
    // intent.putExtra(KEY_SEARCH_TYPE, KEY_DEVICE_SEARCH);
    // startActivity(intent);
    // return true;
    // default:
    // return super.onOptionsItemSelected(item);
    // }
}

@Override
public void onTabChanged(String tabId) {

    /************ Called when tab changed *************/

    // ********* Check current selected tab and change according images
    // *******/

    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
        if (i == 0)
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.tab1);
        else if (i == 1)
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.tab2);
        else if (i == 2)
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.tab3);
    }

    Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());

    if (tabHost.getCurrentTab() == 0)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                .setBackgroundResource(R.drawable.tab1_over);
    else if (tabHost.getCurrentTab() == 1)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                .setBackgroundResource(R.drawable.tab2_over);
    else if (tabHost.getCurrentTab() == 2)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                .setBackgroundResource(R.drawable.tab3_over);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

/***
 * Called when invalidateOptionsMenu() is triggered
 */
@SuppressLint("NewApi")
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(tabHost);
    // smenu.findItem(R.id.action_welcome).setVisible(!drawerOpen);
    // menu.findItem(R.id.action_welcome).setTitle("Welcome Beena Sarkar!");
    // menu.findItem(R.id.action_last_login).setTitle("Last logged in");
    return super.onPrepareOptionsMenu(menu);
}

}

它只是在主屏幕中实现标签主机,但是当用户点击应用程序图标时我想要标签主机。我只想在导航抽屉上实现标签主机而不是列表。也看不到应用程序图标,我可以点击它来获取我的导航选项。我不知道发生了什么。我用列表创建了导航,但是使用标签主机我遇到了这个问题。 提前谢谢。

0 个答案:

没有答案