调整自定义菜单项 [英] Resize custom menu item

查看:221
本文介绍了调整自定义菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助重新大小我现在的菜单项。我在 TableLayout 创建了以编程方式添加项自定义菜单。这是我目前的菜单:

I need help to re-size my current menu item. I created a custom menu with programmatically added items in a TableLayout. This is my current menu:

我需要改变的第一个菜单的高度。现在的问题是,当我改变第一菜单项的高度的整个菜单改变其高度。我想这样的菜单:

I need to change the height of the first menu. Now the problem is that when I change the height of the first menu item the entire menu changes its height. I want menu like this:

有什么办法重新大小我目前的项目?

Is there any way to re-size my current item?

public synchronized void show(View v) {
    mIsShowing = true;
    boolean isLandscape = false;
    int itemCount = mMenuItems.size();
    if (itemCount<1) return; //no menu items to show
    if (mPopupWindow != null) return; //already showing
    Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    if (display.getWidth() > display.getHeight()) isLandscape = true;
    View mView= mLayoutInflater.inflate(R.layout.custom_menu, null);
    mPopupWindow = new PopupWindow(mView,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, false);
    mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
    mPopupWindow.setWidth(display.getWidth());
    mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

    int divisor = mItemsPerLineInPortraitOrientation;
    if (isLandscape) divisor = mItemsPerLineInLandscapeOrientation;
    int remainder = 0;
    if (itemCount < divisor) {
        mRows = 1;
        remainder = itemCount;
    } else {
        mRows = (itemCount / divisor);
        remainder = itemCount % divisor;
        if (remainder != 0) mRows++;
    }
    TableLayout table = (TableLayout)mView.findViewById(R.id.custom_menu_table);
    table.removeAllViews();

    for (int i=0; i < mRows; i++) {
        TableRow row = null;
        TextView tv = null;
        ImageView iv = null;

        row = new TableRow(mContext);
        row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        for (int j=0; j< divisor; j++) {
            if (i*divisor+j >= itemCount) break;
            final CustomMenuItem cmi = mMenuItems.get(i*divisor+j);
            View itemLayout = mLayoutInflater.inflate(R.layout.custom_menu_item, null);
            if  (j==0)
            {
                itemLayout.setBackgroundResource(R.drawable.current_menu);
                itemLayout.setPadding(5, 5, 5, 5);
            }

            tv = (TextView)itemLayout.findViewById(R.id.custom_menu_item_caption);
            tv.setText(cmi.getCaption());
            iv = (ImageView)itemLayout.findViewById(R.id.custom_menu_item_icon);
            iv.setImageResource(cmi.getImageResourceId());
            itemLayout.setOnClickListener( new OnClickListener() {
               @Override
               public void onClick(View v) {
                    mListener.MenuItemSelectedEvent(cmi);
                    if (mHideOnSelect) hide();
               }
            });
            row.addView(itemLayout);
        }
        table.addView(row);
    }
}

如果我给后台整行比结果

如果给后台布局(每个菜单项),由该行itemLayout.setBackgroundResource(R.drawable.menubackground);

if i give background to entire row than result if give background to layout(each menu item) by this line itemLayout.setBackgroundResource(R.drawable.menubackground);

但我想是这样的菜单

But i want look like this menu

推荐答案

一个简单的方法,做你想做的是使菜单 TableLayout 比正常大一点(应该是一样高的所选择的菜单高度会),并添加一些顶级的填充或保证金这是不是当前选择的任何其它菜单项。所以,一个项目总是比这将有一点空间在顶部所有其他项目高。

A simple way to do what you want would be to make the menu TableLayout a bit bigger than normal(it should be as tall as the selected menu height will be) and add some top padding or margin to any other menu item which isn't the current selected one. So one item will always be taller than all other items which will have a bit of space at the top.

编辑:请使用正确的的LayoutParams 尤其 TableLayout 工作,当的TableRow

Please use the proper LayoutParams especially when working with TableLayout and TableRow:

row = new TableRow(mContext);
row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
// ...
View itemLayout = mLayoutInflater.inflate(R.layout.custom_menu_item, row, false);

尝试使用保证金,而不是填充:

Try to use margin instead of padding:

//...
for (int j=0; j< divisor; j++) {
   if (i*divisor+j >= itemCount) break;
   final CustomMenuItem cmi = mMenuItems.get(i*divisor+j);
   View itemLayout = mLayoutInflater.inflate(R.layout.custom_menu_item, row, false);
   TableRow.Layoutparams lp = (TableRow.LayoutParams)itemLayout.getLayoutParams(); 
   if  (j==0) {
     lp.topMargin = 0; 
     itemLayout.setBackgroundResource(R.drawable.current_menu);
     itemLayout.setPadding(5, 5, 5, 5);
   } else {
     lp.topMargin = 12;
   }
   // ...
   itemLayout.setOnClickListener( new OnClickListener() {
       @Override
       public void onClick(View v) {
             mListener.MenuItemSelectedEvent(cmi);
             if (mHideOnSelect) hide();
             TableRow parent = (TableRow) v.getParent();
             final int count = parent.getChildCount();
             for (int i = 0; i < count; i++) {
                 final View child = parent.getChild(i);  
                 final TableRow.LayoutParams lp = (TableRow.LayoutParams) child.getLayoutParams();
                 if (child == v) {
                      lp.topMargin = 0;
                 } else {
                      lp.topMargin = 12;
                 }       
             } 
       }
    });

关于细胞之间的空间,我没有看到,甚至在你的图像。

Regarding space between cells, I don't see that, not even in your images.

这篇关于调整自定义菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆