2014-07-04 92 views

回答

0

只需創建您自己的視圖並將其設置爲您的ActionBarsetCustomView()

你無法達到你想要的標準佈局。對於大量的例子look here

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ActionBar actionBar = getActionBar(); 
    // add the custom view to the action bar 
    actionBar.setCustomView(R.layout.actionbar_view); 
    EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield); 
    search.setOnEditorActionListener(new OnEditorActionListener() { 

     @Override 
     public boolean onEditorAction(TextView v, int actionId, 
      KeyEvent event) { 
     Toast.makeText(MainActivity.this, "Search triggered", 
      Toast.LENGTH_LONG).show(); 
     return false; 
     } 
    }); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM 
     | ActionBar.DISPLAY_SHOW_HOME); 
    } 

} 

來源:vogella.com

+0

看起來很有趣,你能不能把任何的例子嗎? – user3805483

+0

我在帖子中添加了一個教程鏈接。 – Blacklight

0

我不不要以爲你可以做到。 我建議你只是創建一個佈局,在你的應用程序中像一個頂欄。 這樣你就可以放任何你想要的東西。

如果您有多個活動需要此頂部欄,請使用片段而不是活動(以避免在打開新活動時自定義頂部欄被動畫)。