[英]CardView toolbars
我有一個包含CardViews的RecyclerView。
我想向每個外觀和行為類似於主工具欄的CardViews添加一個工具欄:
[圖標] [標題] .......... [按鈕] [按鈕] [菜單]
我從這里( http://blog.grafixartist.com/create-a-card-toolbar/ )看到,可以在CardView中設置實際的android.support.v7.widget.Toolbar對象。 但是它依靠setSupportActionBar(...)來膨脹菜單並響應動作。
您是否認為有可能在我的每個CardView中重現該行為?
你不需要setSupportActionBar
您cardviews與工具欄,直到你將需要設置一些動作條的具體行動上/后退導航操作。
看一下這個好例子(鏈接到下面的整個頁面):
感謝Gabriele的所有幫助。 這是工作代碼:
活動內容:
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mai); Toolbar toolbar = (Toolbar) findViewById(R.id.card_toolbar); toolbar.setTitle("Card Toolbar"); if (toolbar != null) { // inflate your menu toolbar.inflateMenu(R.menu.main); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { return true; } }); } Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main); if (toolbar != null) { // inflate your menu maintoolbar.inflateMenu(R.menu.main); maintoolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { return true; } }); } } }
布局XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar_main" android:layout_width="match_parent" android:layout_height="@dimen/action_bar_size_x2" android:background="@android:color/holo_orange_dark" android:minHeight="?attr/actionBarSize" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar_main" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="@dimen/action_bar_size" android:orientation="vertical" > <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v7.widget.Toolbar android:id="@+id/card_toolbar" android:layout_width="match_parent" android:layout_height="@dimen/action_bar_size" android:background="@android:color/white" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/app_name" android:textSize="24sp" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> </RelativeLayout>
確保您的活動主題正在擴展
Theme.AppCompat.Light.NoActionBar
。這是它的樣子:
幾件事要注意:
- 如果您使用的是卡片高程,則需要在邊距頂上做祭壇,以使卡片與主工具欄對齊
- 我仍然看到主工具欄和卡片工具欄底部之間有1-2像素的邊距。 不知道在這種情況下該怎么辦。 現在,我正在手動調整它。
請注意,本文的作者根本沒有使用過,所以我很確定您也不需要用它來實現您的想法
希望對你有幫助
當然。 setSupportActionBar
用於設置setTitle
目標,菜單setSupportActionBar
和向上/向后操作。 您可以在任何需要的地方使用工具欄,而無需使用setSupportActionBar
。 它的行為類似於ViewGroup。
先前的答案是正確的,但我想提及一下,有時菜單項會重復。 為了解決這個問題,您需要通過調用toolBar.getMenu().clear();
每個CardView
的工具欄菜單toolBar.getMenu().clear();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.