我有一个包含 CardView 的 RecyclerView。
我想向每个 CardView 添加一个工具栏,其外观和行为类似于主工具栏:
[图标] [标题]......... [按钮] [按钮] [菜单]
我从这里看到了(http://blog.grafixartist.com/create-a-card-toolbar/ http://blog.grafixartist.com/create-a-card-toolbar/)可以在 CardView 中设置实际的 android.support.v7.widget.Toolbar 对象。但它依赖 setSupportActionBar(...) 来扩展菜单并响应操作。
您认为可以在我的每个 CardView 中重现该行为吗?
你不需要setSupportActionBar
对于带有工具栏的卡片视图,直到您需要设置一些 ActionBar 特定操作向上/后退导航操作。
看看这个很好的例子(链接到下面的整个页面):
感谢加布里埃尔的所有帮助。这是工作代码:
活动 :
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 像素的边距。不知道在这种情况下该怎么办。现在,我
我手动对齐它。
From: 如何使用 appcompat v7 创建卡片工具栏 https://stackoverflow.com/questions/26848843/how-to-create-a-card-toolbar-using-appcompat-v7
请注意,这篇文章的作者根本没有使用过它,所以我很确定您也不需要它来实现您的想法
希望有帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)