How to add dividers between specific menu items?

后端 未结 7 1904
旧巷少年郎
旧巷少年郎 2020-12-03 01:35

Background

I have a menu item in the action bar (toolbar actually) that when clicked, shows a list of items to choose from, similar to radio-buttons:



        
7条回答
  •  遥遥无期
    2020-12-03 01:47

    OK, I've found a nice workaround, but I'm not sure the styling should be this way. That's what I'm missing:

    1. background of items is on top of the background of the popup of the spinner, and I'm not sure if that's the correct way to put it.
    2. I used the white background of the support library for the popup of the spinner. I think there should be a better way to make it white.
    3. I need to know what is the correct style of the divider. for now I used a simple one
    4. Action bar item style is missing. I just used a simple ImageView, and I think it should be different.
    5. For some reason, on some Android versions (maybe Lollipop and below) the background of the items look black instead of white.
    6. The spinner might sometimes have issues with setOnItemSelectedListener , not sure when.

    MainActivity

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        final MenuItem item = menu.findItem(R.id.action_settings);
        final Spinner spinner = ((Spinner) MenuItemCompat.getActionView(item));
        SimpleImageArrayAdapter adapter = new SimpleImageArrayAdapter(this);
        spinner.setAdapter(adapter);
        return true;
    }
    
    public class SimpleImageArrayAdapter extends ArrayAdapter {
        private final String[] items = {"item 1", "item 2", "item 3", "extra item"};
    
        public SimpleImageArrayAdapter(Context context) {
            super(context, 0);
        }
    
        @Override
        public int getCount() {
            return items.length;
        }
    
        @Override
        public String getItem(final int position) {
            return items[position];
        }
    
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View rootView = convertView == null ? LayoutInflater.from(getContext()).inflate(R.layout.spinner_item, parent, false) : convertView;
            TextView tv = (TextView) rootView.findViewById(android.R.id.text1);
            tv.setTextColor(0xff000000);
            tv.setText(items[position]);
            boolean isLastItem = position == getCount() - 1;
            rootView.findViewById(R.id.action_divider).setVisibility(isLastItem ? View.VISIBLE : View.GONE);
            rootView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            return rootView;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //this is the view that's shown for the spinner when it's closed
            ImageView iv = new ImageView(getContext());
            iv.setImageResource(android.R.drawable.ic_menu_add);
            int viewSize = getDimensionFromAttribute(MainActivity.this, android.support.v7.appcompat.R.attr.actionBarSize);
            iv.setLayoutParams(new ViewGroup.LayoutParams(viewSize, viewSize));
            iv.setScaleType(ScaleType.CENTER_INSIDE);
            iv.setBackgroundResource(getResIdFromAttribute(MainActivity.this, R.attr.selectableItemBackground));
            return iv;
        }
    
    }
    
    public static int getResIdFromAttribute(final Activity activity, final int attr) {
        if (attr == 0)
            return 0;
        final TypedValue typedValue = new TypedValue();
        activity.getTheme().resolveAttribute(attr, typedValue, true);
        return typedValue.resourceId;
    }
    
    public static int getDimensionFromAttribute(final Context context, final int attr) {
        final TypedValue typedValue = new TypedValue();
        if (context.getTheme().resolveAttribute(attr, typedValue, true))
            return TypedValue.complexToDimensionPixelSize(typedValue.data, context.getResources().getDisplayMetrics());
        return 0;
    }
    

    res/menu/menu_main.xml

    
        
    
    

    res/layout/spinner_item.xml

    
    
    
        
    
        
    
    
    

    res/layout/spinner.xml

    
    
    

    res/values/styles.xml

    
    

    res/drawable/divider.xml

    
    
        
        
    
    

提交回复
热议问题