关于SO的许多答案似乎表明不可能在溢出菜单列表中添加图标,但是许多应用程序,例如联系人,(见图)不仅做到了这一点,而且还有一个相当不错的带圆角的边框,通过向上滑动使其显示。我怎么能这样做?标准menuinflater仅向操作栏添加图标
答案 0 :(得分:0)
您需要创建一个用于表示每个选项的布局,并将其与适配器放在一起。
我这样做:
<强>布局:强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingBottom="5sp"
android:paddingLeft="5sp"
android:paddingTop="5sp" >
<ImageView
android:id="@+id/dliIVImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:maxHeight="20sp"
android:maxWidth="20sp"
android:src="@drawable/icon" />
<TextView
android:id="@+id/dliLblOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#fff" />
</LinearLayout>
<强>适配器:强>
public class MenuAdapter extends BaseAdapter {
private String[] options;
private LayoutInflater mInflater;
private ViewHolder holder;
public static final Integer[] images = { R.drawable.icon_search,
R.drawable.icon_profile, R.drawable.icon_password, R.drawable.icon_locate, R.drawable.icon_logout};
static class ViewHolder{
private TextView option;
private ImageView img;
}
public MenuAdapter(Context context, String[] options) {
mInflater = LayoutInflater.from(context);
this.options = options;
}
@Override
public int getCount() {
return options.length;
}
@Override
public Object getItem(int index) {
return options[index];
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int posicao, View convertView, ViewGroup arg2) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
holder = new ViewHolder();
holder.option = (TextView) convertView.findViewById(R.id.dliLblOption);
holder.img = (ImageView) convertView.findViewById(R.id.dliIVImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.option.setText(options[posicao]);
holder.img.setImageResource(images[posicao]);
return convertView;
}
}
<强>活动:强>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
opcoes = ["option1", "option2", "option3"];
MenuAdapter mAdapter = new MenuAdapter(getApplicationContext(), opcoes);
menuLateral = (DrawerLayout) findViewById(R.id.drawer_layout);
listaMenuLateral = (ListView) findViewById(R.id.left_drawer);
listaMenuLateral.setAdapter(mAdapter);
}
主要布局
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>