简体   繁体   English

在导航抽屉上添加图标

[英]Adding icon on navigation drawer

I am already done in doing the navigation drawer but i want to add icons beside the title of the navigation list but i dont know how. 我已经做完导航抽屉了,但是我想在导航列表的标题旁边添加图标,但是我不知道如何。 I already searched many tutorials but they are different to my code. 我已经搜索了许多教程,但是它们与我的代码不同。 My navigation drawer is my own creation not the drawer activity of android studio thats why i have hard time to find answers on how to add icons. 我的导航抽屉是我自己创建的,而不是android studio的抽屉活动,这就是为什么我很难找到有关如何添加图标的答案的原因。 this is my code. 这是我的代码。

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);
    }
}

This is my XML. 这是我的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>

Instead of having simple array adapter, You can have a custom ArrayAdapter which has ImageView and TextView next to it. 您可以拥有一个自定义ArrayAdapter,而不是简单的数组适配器,该适配器旁边具有ImageView和TextView。

There are lot of tutorials on how to write custom ArrayAdapter. 关于如何编写自定义ArrayAdapter的教程很多。

example: 例:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM