Java – how to add icons to items in the navigation drawer
•
Java
I want to place icons next to items in my navigation drawer. I have set such icons:
Titles = getResources().getStringArray(R.array.array1); Icons = getResources().getIntArray(R.array.icons); mDrawerLayout = (DrawerLayout)findViewById(R.id.Welcome); mDrawerList = (ListView) findViewById(R.id.left_drawer); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START); mDrawerList.setAdapter( new ArrayAdapter<String>(this,R.layout.drawer_list_item,Titles));
I know that this process must involve adding an image view to the drawer_ list_ Item's text view in XML, but I don't know how to do this What is the best way to accomplish this task?
This is my drawer_ list_ item. xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:paddingLeft="16dp" android:paddingRight="16dp" android:textColor="#111" android:background="?android:attr/activatedBackgroundIndicator" android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
Solution
The navigation drawer is essentially a list view Create a drawer using any layout you want (text ImageView)_ item. XML and pass it to arrayadapter Then, when populating the listview (in the getview method), assign the ImageView to the drawable
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码