在导航抽屉上添加图标

时间:2015-08-30 08:53:35

标签: android navigation-drawer

我已经完成了导航抽屉,但我想在导航列表的标题旁边添加图标,但我不知道如何。我已经搜索了很多教程,但它们与我的代码不同。我的导航抽屉是我自己的创作而不是android工作室的抽屉活动,这就是为什么我很难找到如何添加图标的答案。这是我的代码。

public class MainActivity extends ActionBarActivity {

    private ListView mDrawerList;
    private DrawerLayout mDrawerLayout;
    private ArrayAdapter<String> mAdapter;
    public ActionBarDrawerToggle mDrawerToggle;
    private String mActivityTitle;
    ProgressDialog progressDialog;
    EditText license;

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

        mDrawerList = (ListView)findViewById(R.id.navList);
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();
        license = (EditText) findViewById(R.id.loglicense);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading");
        progressDialog.setCancelable(false);

        addDrawerItems();
        setupDrawer();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    private void addDrawerItems() {
        String[] Array = { "Driver Details", "List of Drivers", "Maps and Route", "Report Driver", "Contacts", "Display Contacts", "Post on Twitter", "Call For Emergency", "Rate Taxi", "Driver's Feedback", "User's Favorites", "Top Drivers", "Top Operators", "Account Settings" };
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Array);
        mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(getBaseContext(), DriverDetails.class));
                        break;
                    case 1:
                        startActivity(new Intent(getBaseContext(), DriversList.class));
                        break;
                    case 2:
                        startActivity(new Intent(getBaseContext(), MapsActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(getBaseContext(), ReportActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(getBaseContext(), Contacts.class));
                        break;
                    case 5:
                        startActivity(new Intent(getBaseContext(), DisplayContact.class));
                        break;
                    case 6:
                        startActivity(new Intent(getBaseContext(), MainActivity2Activity.class));
                        break;
                    case 7:
                        startActivity(new Intent(getBaseContext(), EmergencyAssistance.class));
                        break;
                    case 8:
                        startActivity(new Intent(getBaseContext(), Rating.class));
                        break;
                    case 9:
                        startActivity(new Intent(getBaseContext(), ListComments.class));
                        break;
                    case 10:
                        startActivity(new Intent(getBaseContext(), ListFavorite.class));
                        break;
                    case 11:
                        startActivity(new Intent(getBaseContext(), TopDriver.class));
                        break;
                    case 12:
                        startActivity(new Intent(getBaseContext(), TopCompany.class));
                        break;
                    case 13:
                        startActivity(new Intent(getBaseContext(), ChangePassword.class));
                        break;
                }
            }
        });
    }

    private void setupDrawer() {
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle("TaxiSafe");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mActivityTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivity(new Intent(getBaseContext(), EmergencyAssistance.class));
            return true;
        }

        // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的XML。

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

<!-- The first child in the layout is for the main Activity UI-->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@drawable/backgroundlahat"
    android:focusable="false">

    </RelativeLayout>

    <!-- Side navigation drawer UI -->
    <ListView
        android:id="@+id/navList"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#ffeeeeee" />

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

2 个答案:

答案 0 :(得分:0)

您可以拥有一个自定义的ArrayAdapter,它旁边有ImageView和TextView,而不是简单的数组适配器。

有很多关于如何编写自定义ArrayAdapter的教程。

示例:

http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter

答案 1 :(得分:0)

这个例子很简单,请检查:EXAMPLE