【问题标题】:How to set a CardView widget above an appBarLayout?如何在 appBarLayout 上方设置 CardView 小部件?
【发布时间】:2015-10-20 07:46:32
【问题描述】:

我正在尝试让卡片小部件从 appBarLayout 上方开始,并在随后的白色背景上方继续。在这张卡片上方,我需要有 ImageView(在顶部中心)和两个浮动按钮(在右下角)。这些元素中有一半在卡片上,另一半不在。

我的问题是:我的卡片下方没有阴影,并且按钮被切成两半。

这是我到目前为止得到的: layout_card_detailed.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_margin="5dp">

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

        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/card_view"
            android:layout_gravity="center"
            card_view:cardCornerRadius="1dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:layout_marginTop="@dimen/card_detail_avatar_margin"
            >


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:padding="@dimen/card_detail_padding"
                android:orientation="vertical">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_width="match_parent"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />

                <TextView
                    android:id="@+id/text_description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/abc_text_size_medium_material"
                    android:layout_marginBottom="@dimen/space_big" />

                <TextView
                    android:id="@+id/text_owner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/abc_text_size_small_material"
                    android:layout_marginBottom="@dimen/space_small" />

                <TextView
                    android:id="@+id/text_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/abc_text_size_small_material"
                    />

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/space_big" />

            </LinearLayout>
        </android.support.v7.widget.CardView>


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:id="@+id/image_avatar"
            android:layout_width="@dimen/avatar_size_detail"
            android:layout_height="@dimen/avatar_size_detail"
            android:src="@drawable/contact"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="-28dp"
        android:gravity="bottom|right">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/fab_margin">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_match"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_dialog_email"
                android:layout_marginRight="@dimen/fab_margin"
                />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_reply"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_dialog_email" />
        </LinearLayout>
    </RelativeLayout>

</FrameLayout>

activity_card_detail.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context="com.myapp.CardDetailActivity" tools:ignore="MergeRootFrame">

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
        android:layout_width="match_parent" android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar android:id="@+id/detail_toolbar"
                android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/layout_margin">
        <include layout="@layout/layout_card_detailed"/>
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    终于弄明白了,我不得不使用 CoordinatorLayout 而不是 FrameLayout。 现在一切正常,除了我的 FAB 阴影以一种突然的方式被切割,遵循原始 android 视图的方形形状。 如果有人作为我感兴趣的想法:-)。

    layout_card_detailed.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/layout_margin">
    
            <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/card_view"
                card_view:cardCornerRadius="1dp"
                android:foreground="?android:attr/selectableItemBackground"
                android:layout_marginTop="@dimen/card_detail_avatar_margin">
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    >
                    <ProgressBar
                        android:id="@+id/progress"
                        android:visibility="visible"
                        style="?android:attr/progressBarStyleSmall"
                        android:layout_width="@dimen/card_progress_size"
                        android:layout_height="@dimen/card_progress_size"
                        android:layout_centerInParent="true"
                        />
                </RelativeLayout>
                <LinearLayout
                    android:id="@+id/content_loading"
                    android:visibility="invisible"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:padding="@dimen/card_detail_padding"
                    android:orientation="vertical">
    
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_height="?attr/actionBarSize"
                        android:layout_width="match_parent"
                        app:popupTheme="@style/AppTheme.PopupOverlay" />
    
                    <TextView
                        android:id="@+id/text_description"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_medium_material"
                        android:layout_marginBottom="@dimen/space_big" />
    
                    <TextView
                        android:id="@+id/text_owner"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_small_material"
                        android:layout_marginBottom="@dimen/space_small" />
    
                    <TextView
                        android:id="@+id/text_networks"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_small_material"
                        />
    
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/space_big" />
    
                </LinearLayout>
            </android.support.v7.widget.CardView>
    
        </RelativeLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_anchor="@id/card_view"
            app:layout_anchorGravity="right|end|bottom"
            android:layout_marginRight="@dimen/fab_margin">
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_match"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_share"
                android:layout_marginRight="@dimen/fab_margin"
                />
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_reply"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_dialog_email"
                />
        </LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            app:layout_anchor="@id/card_view"
            app:layout_anchorGravity="right|end|bottom"
            android:layout_marginRight="@dimen/fab_margin">
    
    
        </LinearLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/layout_margin"
            >
            <ImageView
                android:id="@+id/image_avatar"
                android:layout_width="@dimen/avatar_size_detail"
                android:layout_height="@dimen/avatar_size_detail"
                android:src="@drawable/contact"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2015-11-15
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      相关资源
      最近更新 更多