如何将图标添加到导航抽屉中的项目
Posted
技术标签:
【中文标题】如何将图标添加到导航抽屉中的项目【英文标题】:How to add icons to items in a navigation drawer 【发布时间】:2014-01-07 04:14:06 【问题描述】:我希望在我的导航抽屉中的项目旁边有图标,我设置如下:
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));
我知道该过程必须涉及将图像视图添加到位于抽屉列表项的 XML 中的文本视图中,但我不知道该怎么做。实现这一目标的最佳方法是什么?
这是我的drawer_list_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_
android:layout_
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"/>
【问题讨论】:
【参考方案1】:导航抽屉本质上是一个列表视图。使用您想要的任何布局(文本+图像视图)创建一个drawer_item.xml,并将其传递给arrayAdapter。然后在填充 listView 时(在 getview 方法中),将 imageView 分配给您选择的可绘制对象。
【讨论】:
【参考方案2】:**R.layout.my drawer_list_item.xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<ImageView
android:id="@+id/flag"
android:layout_
android:layout_
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/country"
android:layout_
android:layout_
android:layout_toRightOf="@id/flag"
android:layout_centerVertical="true"
android:textSize="15sp" />
<TextView
android:id="@+id/count"
android:layout_
android:layout_
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="0dp"
android:background="@color/DarkGray"
android:textSize="15sp" />
</RelativeLayout>
**java code**
private SimpleAdapter mAdapter;
private List<HashMap<String,String>> mList ;
final private String ITEM = "item";
final private String FLAG = "flag";
final private String COUNT = "count";
String[] mCountries =new String[]"item 1","item 2","item3" ;
int[] mFlags = new int[]
R.drawable.abc_ic_clear,
R.drawable.abc_ic_clear,
R.drawable.abc_ic_clear,
;
String[] mCount = new String[]
"1", "2", "3" ;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
mList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<3;i++)
HashMap<String, String> hm = new HashMap<String,String>();
hm.put(ITEM, mCountries[i]);
hm.put(COUNT, mCount[i]);
hm.put(FLAG, Integer.toString(mFlags[i]) );
mList.add(hm);
**set adapter for ListView**
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
selectItem(position);
);
String[] from = FLAG,ITEM,COUNT ;
int[] to = R.id.flag , R.id.country , R.id.count;
mAdapter = new SimpleAdapter(getActionBar().getThemedContext(), mList, R.layout.my drawer_list_item, from, to);
mDrawerListView.setAdapter(mAdapter);
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
【讨论】:
以上是关于如何将图标添加到导航抽屉中的项目的主要内容,如果未能解决你的问题,请参考以下文章