I downloaded the sample app for the Navigation Drawer from
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Now I'd like to add an icon to a specific item in the list; for example
How can I do this? (Code please)
Assuming you are implementing the Navigation Drawer
by a ListView
, you will need to modify the layout for the list item by adding an ImageView. Then you should modify the adapter you use to populate the ListView
so that it sets the src
of the ImageView
accordingly.
Quoting the guide you linked:
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
That's where most of your changes will be wired: specifying a layout containing a TextView and and ImageView and creating a new Adapter.
As a convenience, you might create a class called something like NavDrawerItem which will have two fields: one for the icon, the other the caption which you'll display through a TextView
.
In your adapter, be sure to consider the menu items for which you won't be displaying an icon.
try this
private ActionBarDrawerToggle mDrawerToggle;
mDrawerToggle=new ActionBarDrawerToggle(this,
mdrawerlayout,
R.drawable.ic_whats_hot,
R.string.app_name,
R.string.app_name)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View view)
{
getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu();
}
};
public boolean onOptionsItemSelected(MenuItem item)
{
if(mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
switch(item.getItemId())
{
case R.id.action_settings:
intent1=new Intent(MainActivity.this,ActivitySetting.class);
startActivity(intent1);
return true;
case R.id.action_websearch:
intent1=new Intent(Intent.ACTION_VIEW,Uri.parse("http://http://www.vogella.com/"));
startActivity(intent1);
return true;
default :
return super.onOptionsItemSelected(item);
}
}
try this in coding and in XML file
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_websearch"
android:showAsAction="always"
android:icon="@drawable/action_search"
android:title="search"/>
<item
android:id="@+id/action_settings"
android:title="Settings"
android:icon="@drawable/ic_launcher"
>
</item>
<item
android:id="@+id/action_logout"
android:title="logout"
android:icon="@drawable/ic_launcher"
/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.