调整自定义菜单项的大小

时间:2013-02-14 07:15:48

标签: android customization android-menu

我需要帮助来重新调整当前菜单项的大小。我在TableLayout中创建了一个自定义菜单,其中包含以编程方式添加的项目。这是我目前的菜单:

this is my current menu

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

i want menu like this

有没有办法重新调整当前项目的大小?

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)给出布局背景(每个菜单项); enter image description here

但我希望看起来像这个菜单 enter image description here

1 个答案:

答案 0 :(得分:1)

一个简单的方法来做你想要的就是让菜单TableLayout比正常大一点(它应该与所选菜单高度一样高)并为任何一个添加一些顶部填充或边距其他菜单项不是当前选定的菜单项。因此,一个项目将始终高于顶部有一点空间的所有其他项目。

修改:请使用正确的LayoutParams,尤其是在使用TableLayoutTableRow时:

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);

尝试使用margin而不是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;
                 }       
             } 
       }
    });

关于单元格之间的空间,我看不到,甚至在图像中都没有。

相关问题