【问题标题】:How to create a card toolbar using appcompat v7如何使用 appcompat v7 创建卡片工具栏
【发布时间】:2015-01-07 01:18:27
【问题描述】:

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

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

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

        <Toolbar/>

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

</RelativeLayout>

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

【问题讨论】:

    标签: android material-design android-support-library android-appcompat android-cardview


    【解决方案1】:

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

    活动:

    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 像素的边距。不知道在这种情况下该怎么做。目前,我正在手动对齐它。

    【讨论】:

    • 嗨.. @dimen/action_bar_size_x2 和 @dimen/action_bar_size 的值是多少。因为我的卡片视图没有与第一个工具栏重叠。
    • x2 是值的两倍,用于操作栏大小。
    • 它不像屏幕截图那样呈现。你确定android:layout_below="@+id/toolbar_main" 吗?我会删除它以使第一个 LinearLayout 从屏幕顶部开始...
    • 是的..这是设备的实际屏幕截图。
    • 我用这个代替手动调整主工具栏的高度:final ViewGroup.LayoutParams lp = mainToolbar.getLayoutParams(); lp.height += ((ViewGroup.MarginLayoutParams) card.getLayoutParams()).topMargin; mainToolbar.setLayoutParams(lp);
    【解决方案2】:

    您可以使用工具栏作为ActionBar、CardView 和卡片内的另一个工具栏(独立)来获取它。

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

    <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: 一个扩展的 toolbar(作为操作栏)和一个如上所述的 CardView,带有边距。

    【讨论】:

    • 我想要相反的(不确定完全相反)..我的卡片视图应该覆盖在工具栏上..工具栏不应该进入卡片视图。
    • 在屏幕中,您有一个用作操作栏的工具栏,以及卡片内的另一个工具栏(独立)。
    • 哦.. 所以基本上你是说我必须使用 2 个工具栏。
    猜你喜欢
    • 2014-12-19
    • 2014-12-22
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2014-12-24
    • 2015-01-29
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多