2013-11-14 3 views
1

Многие ответы на SO, похоже, предполагают, что добавлять список значков в список меню переполнения невозможно, однако ряд приложений, например «Контакты» (см. Изображение), сделали не только это но также имеют довольно приятную границу с закругленными углами и заставляют ее появляться при движении вверх. Как я могу сделать то же самое? Стандарт menuinflater только добавляет значки на панели действийДобавить значки в список меню.

Contacts showing menu with icons

ответ

0

Вам необходимо создать макет для каждого варианта представляет и положить, что с 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"/> 

+0

Спасибо за очень быстрый ответ, однако, я борюсь немного. –

+0

1. Я не могу найти DrawerLayout. Я думал, что это было в android-support-v4, но я не могу его импортировать. 2. Что такое R.id.drawer_layout и R.id.left_drawer –

+0

Я также не понимаю код, который у вас есть в методе onCreate(). Благодаря! –

Смежные вопросы