20

我想按照材料设计指南中的建议创建如下图所示的工具栏:

在此处输入图像描述

我可以通过使用具有相对布局的工具栏来实现这一点:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Toolbar/>

        <LinearLayout android:layout_marginTop="-17dp" />

</RelativeLayout>

我不确定这是否正确。任何帮助表示赞赏。

4

2 回答 2

22

感谢加布里埃尔的所有帮助。这是工作代码:

活动 :

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);
        if (toolbar != null) {
            // inflate your menu
            toolbar.inflateMenu(R.menu.main);
            toolbar.setTitle("Card Toolbar");
            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    return true;
                }
            });
        }

        Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main);
        if (maintoolbar != 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 像素的边距。不知道在这种情况下该怎么做。现在,我正在手动对齐它。
于 2014-11-10T18:39:07.553 回答
11

您可以使用作为ActionBar的 Toolbar 、 CardView 和 Card 内的另一个 Toolbar(standalone) 来获取它。

对于卡片内的独立工具栏,您可以使用以下内容:

<android.support.v7.widget.CardView>

   <LinearLayout>

        <Toolbar  android:id="@+id/card_toolbar" />

        //......

   </LinearLayout>

</CardView>

然后你可以膨胀你的菜单来获得图标动作。

 Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.card_toolbar);
   if (toolbar != null) {
         //inflate your menu    
         toolbar.inflateMenu(R.menu.card_toolbar);
         toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                         //.....
                    }
                });
   }

对于用作操作栏的工具栏和主布局,您可以使用:

选项1:

<RelativeLayout>

    <toolbar/>  //Main toolbar 

    <View
        android:background="@color/primary_color"
        android:layout_height="@dimen/subtoolbar_height"/>

    <CardView /> //described above

</RelativeLayout>

选项 2:一个扩展的工具栏(作为操作栏)和一个如上所述的 CardView 使用边距。

于 2014-11-10T17:03:53.187 回答