【问题标题】:Create the Layout with CardView and Floating Action Button Android使用 CardView 和浮动操作按钮 Android 创建布局
【发布时间】:2015-09-23 04:31:36
【问题描述】:

我想使用 Material CardView 和自定义浮动操作按钮创建如下图所示的布局:

我被开发到这个 Layout xml 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/mainview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:id="@+id/cv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="2dp"
            app:cardElevation="2dp"
            android:layout_marginBottom="@dimen/cardMarginVertical"
            android:layout_marginLeft="@dimen/cardMarginHorizontal"
            android:layout_marginRight="@dimen/cardMarginHorizontal"
            android:layout_marginTop="@dimen/cardMarginVertical"
            app:cardPreventCornerOverlap="false"
            app:contentPadding="0dp">

            <com.github.florent37.materialviewpager.sample.ExpandableTextView
                android:id="@+id/expandingTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:text="@string/longText" />

            <RelativeLayout
                android:id="@+id/buttonlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/expandingTextView"
                android:layout_gravity="bottom"
                android:background="@android:color/transparent">

                <com.getbase.floatingactionbutton.FloatingActionButton
                    android:id="@+id/action_c"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="#fff"
                    android:layout_alignParentRight="true"
                    fab:fab_colorPressed="#fff"
                    android:visibility="visible" />
            </RelativeLayout>
        </android.support.v7.widget.CardView>
    </RelativeLayout>
</FrameLayout>

【问题讨论】:

    标签: android android-layout material-design


    【解决方案1】:

    要实现像您的图片一样的 UI,下面的一些代码会有所帮助。

    FloatingActionButton 中的参数layout_anchorlayout_anchorGravity 是重点。

    <?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">
    
        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <android.support.v7.widget.CardView
                android:id="@+id/cv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp">
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level. This design means that your applications can use the libraries' features and still be compatible with devices running Android 1.6 (API level 4) and up." />
    
            </android.support.v7.widget.CardView>
    
        </android.support.v4.widget.NestedScrollView>
    
        <android.support.design.widget.FloatingActionButton
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            app:layout_anchor="@id/cv"
            app:layout_anchorGravity="right|end|bottom"
            android:layout_margin="16dp"
            android:clickable="true"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    编辑:

    早期答案在 Android 5.0.1 和 5.0.2 中存在一些错误,FAB 将显示在 CardView 下方。

    CardView 之外添加NestedScrollViewlayout_behavior 以在Android 5.0.1 和5.0.2 中工作

    【讨论】:

    • 你能像上面那样在我发布的代码中更改一个代码,这样我就可以轻松地做到这一点。感谢支持。
    • 我从布局中删除了一些无用的代码。你可以看看。
    • 我得到了 android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.CoordinatorLayout in my layout by replace the RelativeLayout to CoordinatorLayout as above question's xml file please帮助我。
    • 哦,我忘了你没有使用支持设计库。更新您的支持库和支持存储库并将com.android.support:design:22.2.0 添加到您的 build.gradle。
    • 我在我的 build.gradle 中添加了 com.android.support:appcompat-v7:21.0.3 它不工作吗? @Ellie Zou
    猜你喜欢
    • 2016-11-27
    • 2018-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多