CardView工具栏 [英] CardView toolbars

查看:100
本文介绍了CardView工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含CardViews的RecyclerView.

I have a RecyclerView that contains CardViews.

我想向每个外观和行为类似于主工具栏的CardViews添加一个工具栏:

I would like to add a Toolbar to each of those CardViews that would look and behave like the main Toolbar:

[图标] [标题] .......... [按钮] [按钮] [菜单]

[Icon] [Title] .......... [Button] [Button] [Menu]

我从这里看到了( http://blog.grafixartist.com/create-a-card-toolbar/),则可以在CardView中设置实际的android.support.v7.widget.Toolbar对象.但是它依靠setSupportActionBar(...)来膨胀菜单并响应动作.

I've seen from here (http://blog.grafixartist.com/create-a-card-toolbar/) that it is possible to set an actual android.support.v7.widget.Toolbar object inside a CardView. But it relies on setSupportActionBar(...) to inflate the menu and respond to actions.

您认为可以在每个CardViews中重现该行为吗?

Do you think it's possible to reproduce that behaviour in each of my CardViews?

推荐答案

使用工具栏在卡片视图中不需要setSupportActionBar,直到需要将一些特定于ActionBar的操作向上/向后导航操作设置为止.

You don't need setSupportActionBar for your cardviews with toolbars until you would need to set some ActionBar specific actions up/back navigation actions.

看看这个好例子(链接到下面的整个页面):

Take a look at this fine example (link to whole page below):

感谢Gabriele的所有帮助.这是工作代码:

Thanks Gabriele for all the help. Here is working code:

活动:

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像素的边距.不知道在这种情况下该怎么做.现在,我 正在手动对齐.
  • If you are using card elevation then you need to altar the margin top so that your card aligns with the main toolbar
  • I am still seeing 1-2 pixel margin between bottom of main toolbar and card toolbar. Not sure about what to do in this case. For now, i am aligning it manually.

来自:如何使用appcompat v7创建名片工具栏

请注意,本文的作者根本没有使用过它,因此,我敢肯定,您也不需要用它来实现您的想法

Notice that author of this post hadn't used it at all, so I'm pretty sure you would also don't need that for implementation your idea

希望有帮助

这篇关于CardView工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆