簡體   English   中英

如何在同一活動中添加導航抽屜和標簽小部件?

[英]how to add navigation drawer and tab widget in same activity?

我想將滑動抽屜添加到操作欄以及該應用程序的選項卡小部件。

我曾經使用過創建導航抽屜

創建導航抽屜的指南。 我試圖添加標簽小部件對此。 但是我做不到。

誰能幫我在操作欄后添加標簽小部件。

像這樣。 布局

這是xml文件。

<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/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

如何為此添加標簽小部件作為提及布局?

anurudhika你應該使用我的代碼,這會幫助你

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

<!-- Framelayout to display Fragments -->

<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="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" >
        </TabWidget>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

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

<!-- Listview to display slider menu -->

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

在TabActivity中編寫以下代碼

public class TabActivity extends FragmentActivity implements OnTabChangeListener {
    TextView txt;

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

    private TabHost mTabHost;
    private HashMap mapTabInfo = new HashMap();
    private TabInfo mLastTab = null;
    public static Stack<Fragment> mStackTabHomeScreen = new Stack<Fragment>();
    public static Stack<Fragment> mStackTabSecondScreen = new Stack<Fragment>();

    private HomeScreen mHomeScreen;
    private SecondHomeScreen mSecondHomeScreen;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

    private class TabInfo {
        private String tag;
        private Class clss;
        private Bundle args;
        private Fragment fragment;

        TabInfo(String tag, Class clazz, Bundle args) {
            this.tag = tag;
            this.clss = clazz;
            this.args = args;
        }
    }

    class TabFactory implements TabContentFactory {
        private final Context mContext;

        /** * @param context */
        public TabFactory(Context context) {
            mContext = context;
        }

        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
        mDrawerLayout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
        R.string.drawer_open, /* "open drawer" description for accessibility */
        R.string.drawer_close /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
        mDrawerToggle.setDrawerIndicatorEnabled(true);

        mHomeScreen = new HomeScreen();
        mSecondHomeScreen = new SecondHomeScreen();
        fragmentManager = getSupportFragmentManager();

        initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content
        // view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch (item.getItemId()) {
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //selectItem(position);

            if (position == 0) {
                selectItem(position);
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                BarcodeScreen mBarcodeScreen = new BarcodeScreen();
                fragmentTransaction.replace(R.id.realtabcontent, mBarcodeScreen);
                TabActivity.mStackTabHomeScreen.add(mBarcodeScreen);
                fragmentTransaction.commit();

                Toast.makeText(getApplicationContext(), "Position : " + position, 1000).show();
                mDrawerLayout.closeDrawer(mDrawerList);
            }
            else if (position == 1) {
                selectItem(position);
                Toast.makeText(getApplicationContext(), "Position : " + position, 1000).show();

                mDrawerLayout.closeDrawer(mDrawerList);
            }
            else if (position == 2) {
                selectItem(position);
                Toast.makeText(getApplicationContext(), "Position : " + position, 1000).show();

                mDrawerLayout.closeDrawer(mDrawerList);
            }
            else if (position == 3) {
                selectItem(position);
                Toast.makeText(getApplicationContext(), "Position : " + position, 1000).show();

                mDrawerLayout.closeDrawer(mDrawerList);
            }

        }
    }

    private void selectItem(int position) {
        // update the main content by replacing fragments
        Fragment fragment = new HomeScreen();
        Bundle args = new Bundle();
        args.putInt(HomeScreen.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);

        //      FragmentManager fragmentManager = getFragmentManager();
        //      fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
                                                                // selected
        super.onSaveInstanceState(outState);
    }

    /**
     * Step 2: Setup TabHost
     */
    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        TabActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("HomeScreen").setIndicator("HomeScreen"), (tabInfo = new TabInfo("HomeScreen", HomeScreen.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);

        TabActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("SecondScreen").setIndicator("SecondScreen"), (tabInfo = new TabInfo("SecondScreen", SecondHomeScreen.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);

        // Default to first tab
        this.onTabChanged("HomeScreen");
        //
        mTabHost.setOnTabChangedListener((OnTabChangeListener) this);
    }

    private static void addTab(TabActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) { // Attach a Tab view
                                                                                                                    // factory to the spec
        tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();
        // 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.

        tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            activity.getSupportFragmentManager().executePendingTransactions();
        }
        tabHost.addTab(tabSpec);
    }

    /**
     * (non-Javadoc) * @see
     * android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
     */
    public void onTabChanged(String tag) {
        TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);

        if (mTabHost.getCurrentTabTag() == "HomeScreen") {

            if (mStackTabHomeScreen.size() > 0) {
                Fragment fragment = mStackTabHomeScreen.get(mStackTabHomeScreen.size() - 1);
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.realtabcontent, fragment);
                fragmentTransaction.commit();
            }
            else {
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.realtabcontent, mHomeScreen);
                mStackTabHomeScreen.add(mHomeScreen);
                fragmentTransaction.commit();

            }

        }
        if (mTabHost.getCurrentTabTag() == "SecondScreen") {

            if (mStackTabSecondScreen.size() > 0) {
                Fragment fragment = mStackTabSecondScreen.get(mStackTabSecondScreen.size() - 1);
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.realtabcontent, fragment);
                fragmentTransaction.commit();
            }
            else {
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.realtabcontent, mSecondHomeScreen);
                mStackTabSecondScreen.add(mSecondHomeScreen);
                fragmentTransaction.commit();

            }
        }

        /*      if (mLastTab != newTab) {
                    FragmentTransaction ft = this.getSupportFragmentManager()
                            .beginTransaction();
                    if (mLastTab != null) {
                        if (mLastTab.fragment != null) {
                            ft.detach(mLastTab.fragment);
                        }
                    }
                    if (newTab != null) {
                        if (newTab.fragment == null) {
                            newTab.fragment = Fragment.instantiate(this,
                                    newTab.clss.getName(), newTab.args);
                            ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                        } else {
                            ft.attach(newTab.fragment);

                        }
                    }
                    mLastTab = newTab;
                    ft.commit();
                    this.getSupportFragmentManager().executePendingTransactions();

                }*/
    }

    @Override
    public void onBackPressed() {

        if (mTabHost.getCurrentTabTag() == "HomeScreen") {

            if (mStackTabHomeScreen.size() > 1) {

                Fragment fragment = mStackTabHomeScreen.get(mStackTabHomeScreen.size() - 2);
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.realtabcontent, fragment);
                mStackTabHomeScreen.pop();
                fragmentTransaction.commit();
            }

        }

        if (mTabHost.getCurrentTabTag() == "SecondScreen") {

            if (mStackTabSecondScreen.size() > 1) {

                Fragment fragment = mStackTabSecondScreen.get(mStackTabSecondScreen.size() - 2);
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.realtabcontent, fragment);
                mStackTabSecondScreen.pop();
                fragmentTransaction.commit();
            }
        }

    }
}

在您的xml中:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <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:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <FrameLayout
                    android:id="@+android:id/realtabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="0" />

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="-4dp"
                    android:layout_weight="0" />
            </LinearLayout>
        </TabHost>

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

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />


    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

希望您能解決您的問題。 祝好運!! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM