use*_*767 3 android android-layout
我有一个包含 CardViews 的 RecyclerView。
我想为每个 CardView 添加一个工具栏,它们的外观和行为都类似于主工具栏:
[图标] [标题] .......... [按钮] [按钮] [菜单]
我从这里看到(http://blog.grafixartist.com/create-a-card-toolbar/ ) 看到,可以在 CardView 中设置实际的 android.support.v7.widget.Toolbar 对象。但它依赖于 setSupportActionBar(...) 来扩充菜单并响应操作。
你认为有可能在我的每个 CardViews 中重现这种行为吗?
在您setSupportActionBar需要设置一些 ActionBar 特定操作向上/后退导航操作之前,您不需要带有工具栏的卡片视图。
看看这个很好的例子(链接到下面的整个页面):
感谢加布里埃尔的所有帮助。这是工作代码:
活动 :
Run Code Online (Sandbox Code Playgroud)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 文件:
Run Code Online (Sandbox Code Playgroud)<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 像素的边距。不确定在这种情况下该怎么做。现在,我正在手动对齐它。
请注意,这篇文章的作者根本没有使用它,所以我很确定你也不需要它来实现你的想法
希望有帮助
| 归档时间: |
|
| 查看次数: |
6577 次 |
| 最近记录: |