1
很多關於SO的答案似乎表明,無法將圖標添加到溢出菜單列表中,但是許多應用程序(例如Contacts)(請參閱圖像)不僅可以做到這一點但也有一個相當不錯的圓角邊框,並通過向上滑動出現。我怎樣才能做到這一點?該標準僅menuinflater圖標添加到動作欄中將圖標添加到菜單列表選項
很多關於SO的答案似乎表明,無法將圖標添加到溢出菜單列表中,但是許多應用程序(例如Contacts)(請參閱圖像)不僅可以做到這一點但也有一個相當不錯的圓角邊框,並通過向上滑動出現。我怎樣才能做到這一點?該標準僅menuinflater圖標添加到動作欄中將圖標添加到菜單列表選項
您需要創建一個佈局代表每個選項,並把與一個adapater。
我做這樣的事情:
佈局:
<?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"/>
感謝一個非常快的回覆,不過,我掙扎了一下。 –
1.我找不到DrawerLayout。我認爲這是在android-support-v4中,但我找不到它來導入它。 2.什麼是R.id.drawer_layout和R.id.left_drawer –
我也不明白你在onCreate()方法中的代碼。謝謝! –