簡體   English   中英

如何將膨脹的自定義視圖與工具欄中的其余菜單項對齊

[英]How do I align an inflated custom view with the rest of the menu items in Toolbar

我真的不知道如何放置它,但我想要的只是一個工具欄(操作欄),就像下面的 WhatsApp 風格一樣。

我想要的風格

到目前為止我設法發展的風格

同時,我使用了 Android 工具欄並擴展了一個自定義視圖(包含購物車按鈕)並使用setCustomView()方法添加它。 我嘗試調整TextViewtextSize屬性,但即使是最合理的大小也無法將TextView工具欄菜單項的其余部分對齊。 有關如何執行此操作的任何幫助? 或者有不同的方法嗎?

如果我理解正確,我建議采用不同的方法,您可以將工具欄設置為 SupportActionBar 並添加自定義圖標,就像在普通 actionBar 中所做的那樣

  1. 將您的工具欄設置為 SupportActionBar

前任。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
  1. 創建一個菜單文件,就像對普通 actionBar 所做的一樣

前任。

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action1"
        android:orderInCategory="100"
        android:title="CUSTOM TITTLE"
        android:icon="@drawable/custom_icon1"
        app:showAsAction="always" />

    <item
        android:id="@+id/action2"
        android:orderInCategory="100"
         android:title="CUSTOM TITTLE"
        android:icon="@drawable/custom_icon2"
        app:showAsAction="ifRoom"  />

   </menu>
  1. 最后使用 onCreateOptionsMenu 和 onOptionsItemSelected 完成設置。

前任。

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.custom_menu, menu);

    .....

    return true;
}

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.action_custom_view:

            .....Your code.....

            break;
        case android.R.id.action_custom_view2:

             .....Your code.....

            break;
    }

    return false;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM