2012-06-02 30 views
7
自定義佈局

我剛開始使用ActionBarSherlock在我的第一個屏幕上建立了一些簡單的應用程序, 我有簡單的名單,我添加了新的菜單項添加新項列表:建設ActionMode在ActionBarSherlock

MenuItem newItem = menu.add("New"); 
newItem.setIcon(R.drawable.ic_compose_inverse) 
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 

現在,當用戶選擇添加一個新的項目我要開始一個新的動作模式,增加新的項目,這個動作模式應該包含一個簡單的佈局與文本框和一個按鈕,所以我創造了這個佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

     <EditText 
      android:id="@+id/text" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:inputType="text" > 
     </EditText> 
     <Button 
      android:id="@+id/addBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/add" /> 
</LinearLayout> 

所以現在我只需要se牛逼該佈局的酒吧在新的動作模式:

newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(MenuItem item) { 
       actionMode = startActionMode(new MyAction(ListEditor.this)); 
       return true; 
      } 
     }); 

,並在我的行動:

private final class MyAction implements ActionMode.Callback { 
    ... 
    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
     View customNav = LayoutInflater.from(context).inflate(R.layout.add_item, null); 
     getSupportActionBar().setCustomView(customNav); 
     getSupportActionBar().setDisplayShowCustomEnabled(true); 
     return true; 
    } 
} 

所以基本上我需要從夏洛特例如ActionModes和CustomNavigation之間的事情,但問題是它將佈局設置爲主欄而不是用於打開操作的新欄。

有什麼建議嗎?

回答

9

您可能想要在ActionMode類中使用名爲「setCustomView」的方法。

所以是這樣的:

newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() { 
     @Override 
     public boolean onMenuItemClick(MenuItem item) { 
      actionMode = startActionMode(new MyAction(ListEditor.this)); 
      actionMode.setCustomView(customNav); 
      return true; 
     } 
    });