关于 SO 的许多答案似乎表明无法将图标添加到溢出菜单列表,但是许多应用程序,例如联系人,(见图)不仅做到了这一点,而且还有一个相当漂亮的圆形边框角落并通过向上滑动使其出现。我该怎么做?标准 menuinflater 仅将图标添加到操作栏
问问题
231 次
1 回答
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"/>
于 2013-11-14T17:33:42.243 回答