2013-02-14 73 views
0

我需要幫助來重新調整我的當前菜單項的大小。我創建了一個自定義菜單,其中編程添加了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

+0

你能發佈你的代碼嗎? – Martin 2013-02-14 07:36:56

回答

1

一個簡單的方法做你想做的是使菜單TableLayout有點比正常大(應該是作爲選擇的菜單高度一樣高將是)並添加一些頂部填充或邊距到任何其他不是當前選定的菜單項。所以一個項目總是比所有其他項目都高,這些項目在頂部會有一些空間。

編輯:

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

嘗試使用保證金,而不是填充:請TableLayoutTableRow工作時,特別是使用適當的LayoutParams

//... 
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; 
       }  
      } 
     } 
    }); 

關於細胞之間的空間,我不看到這一點,即使在你的圖像。

+0

感謝您的回覆!我嘗試通過setPadding()方法添加填充。但問題無法解決。有什麼方法可以縮放/調整視圖大小,因爲所有項目都有單獨的視圖。最後添加在tablelayout中。 – 2013-02-14 07:57:30

+0

@BhavinChauhan *但問題無法解決* - 不是很有用。你能解釋一下你在哪裏叫這個,菜單是怎麼樣的(把你的問題放在代碼中)? – Luksprog 2013-02-14 08:02:56

+0

謝謝!我添加了有問題的代碼。我需要另一個幫助。有沒有辦法刪除tablelayout中2個單元格之間的空格? – 2013-02-14 09:05:12