如何在Android应用程序抽屉中添加按钮,开关等?

时间:2014-03-11 05:10:54

标签: java android eclipse android-activity android-actionbar

如何在Android应用程序抽屉中添加按钮,开关,搜索栏等? 我知道如何将文本添加到列表视图中,但我发现上述内容非常困难。我下面有我当前的app抽屉。你会建议做些什么改变来实现上述目标。

代码:

MainActivity

   public class MainActivity extends Activity {

        // Within which the entire activity is enclosed
        DrawerLayout mDrawerLayout;

        // ListView represents Navigation Drawer
        ListView mDrawerList;

        // ActionBarDrawerToggle indicates the presence of Navigation Drawer in the action bar
        ActionBarDrawerToggle mDrawerToggle;

        // Title of the action bar
        String mTitle="";

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mTitle = (String) getTitle();

            // Getting reference to the DrawerLayout
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

            mDrawerList = (ListView) findViewById(R.id.drawer_list);

            // Getting reference to the ActionBarDrawerToggle
            mDrawerToggle = new ActionBarDrawerToggle( this,
                mDrawerLayout,
                R.drawable.ic_drawer,
                R.string.drawer_open,
                R.string.drawer_close){

                    /** Called when drawer is closed */
                    public void onDrawerClosed(View view) {
                        getActionBar().setTitle(mTitle);
                        invalidateOptionsMenu();
                    }

                    /** Called when a drawer is opened */
                    public void onDrawerOpened(View drawerView) {
                        getActionBar().setTitle("Select a river");
                        invalidateOptionsMenu();
                    }
            };

            // Setting DrawerToggle on DrawerLayout
            mDrawerLayout.setDrawerListener(mDrawerToggle);

            // Creating an ArrayAdapter to add items to the listview mDrawerList
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(),
                R.layout.drawer_list_item ,
                getResources().getStringArray(R.array.rivers)
            );

            // Setting the adapter on mDrawerList
            mDrawerList.setAdapter(adapter);

            // Enabling Home button
            getActionBar().setHomeButtonEnabled(true);

            // Enabling Up navigation
            getActionBar().setDisplayHomeAsUpEnabled(true);

            // Setting item click listener for the listview mDrawerList
            mDrawerList.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent,
                    View view,
                    int position,
                    long id) {

                        // Getting an array of rivers
                        String[] rivers = getResources().getStringArray(R.array.rivers);

                        //Currently selected river
                        mTitle = rivers[position];

                        // Creating a fragment object
                        RiverFragment rFragment = new RiverFragment();

                        // Creating a Bundle object
                        Bundle data = new Bundle();

                        // Setting the index of the currently selected item of mDrawerList
                        data.putInt("position", position);

                        // Setting the position to the fragment
                        rFragment.setArguments(data);

                        // Getting reference to the FragmentManager
                        FragmentManager fragmentManager = getFragmentManager();

                        // Creating a fragment transaction
                        FragmentTransaction ft = fragmentManager.beginTransaction();

                        // Adding a fragment to the fragment transaction
                        ft.replace(R.id.content_frame, rFragment);

                        // Committing the transaction
                        ft.commit();

                        // Closing the drawer
                        mDrawerLayout.closeDrawer(mDrawerList);
                }
            });
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            mDrawerToggle.syncState();
        }

        /** Handling the touch event of app icon */
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

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

            menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
            return super.onPrepareOptionsMenu(menu);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    }

Activity_Layout

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

1 个答案:

答案 0 :(得分:0)

您可以将此库用于抽屉library

或者如果你想要这个抽屉然后放一个linearlayout然后把你的视图放在那里。

 SlidingMenu menu = new SlidingMenu(this);
 menu.setMode(SlidingMenu.LEFT);
 menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
 menu.setShadowWidthRes(R.dimen.shadow_width);
 menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
 menu.setFadeDegree(0.35f);
 menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
 menu.setMenu(R.layout.side_panel);

现在在侧面板布局中将您的视图放在一边,现在视图侧面板使用此:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
        if (menu != null) {
            menu.toggle();
        } 
        break;
    }
    return true;
}

用于初始化侧面板数据使用:

menu.findViewById(R.id.your_view_id).setOnClickListener(this);

并覆盖你想要的onclick事件和句柄。

有关详细信息,请参阅github中的示例。