标签: android-coordinatorlayout

如何在两个小部件/布局之间添加新的"浮动操作按钮"

我想你已经看到了新的Android设计指南,新的"浮动动作按钮"又名"FAB"

例如这个粉红色的按钮:

在此输入图像描述

我的问题听起来很愚蠢,我已经尝试了很多东西,但是将这个按钮放在两个布局的交叉点上的最佳方法是什么?

在上面的例子中,这个按钮完全位于我们可以想象的ImageView和relativeLayout之间.

我已经尝试了很多调整,但我确信有一个正确的方法来做到这一点.

android floating-action-button android-coordinatorlayout

282
推荐指数
9
解决办法
32万
查看次数

在CoordinatorLayout中添加工具栏下方的视图

我有以下布局:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

我添加了Fragments FrameLayout,替换它们.我的一个Fragment的一个是列表,它具有以下布局:

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Run Code Online (Sandbox Code Playgroud)

我的问题是工具栏是在列表上绘制的.我试图通过将内容包装CoordinatorLayout成a LinearLayout来解决这个问题,这解决了透支问题,但这样appbar滚动行为就不再起作用了.

任何帮助深表感谢!

android android-toolbar androiddesignsupport android-coordinatorlayout

162
推荐指数
2
解决办法
9万
查看次数

错误:程序类型已存在:android.support.design.widget.CoordinatorLayout $ Behavior

我在构建项目时遇到以下错误.在这个项目中没有使用CoordinatorLayout.刚刚在build.gradle中添加为依赖项:

我使用的是Android Studio 3.2 Canary 4.

logcat的

AGPBI:{"kind":"error","text":"程序类型已存在:android.support.design.widget.CoordinatorLayout $ Behavior","sources":[{}],"tool":"D8" }:app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED FAILURE:构建因异常而失败.*出了什么问题:任务执行失败':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

com.android.builder.dexing.DexArchiveMergerException:合并dex存档时出错:/windows/Other/app/build/intermediates/transforms/dexBuilder/debug/0.jar,/ windows/Other/app/build/intermediates/transforms/dexBuilder/debug/1.jar,/windows/Other/app/build/intermediates/transforms/dexBuilder/debug/4.jar,.....................

/windows/Other/app/build/intermediates/transforms/dexBuilder/debug/294.jar

程序类型已存在:android.support.design.widget.CoordinatorLayout $ Behavior

的build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "com.dagkot"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String", "BASE_URL", "\"http://api.openweathermap.org/data/2.5/\""
            buildConfigField "String", "API_KEY", "\"435e9075f348868c2714fe7c6895efa5\""
        }
        debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", …
Run Code Online (Sandbox Code Playgroud)

android kotlin dagger-2 android-coordinatorlayout

127
推荐指数
9
解决办法
17万
查看次数

如何模仿谷歌地图的底层3阶段行为?

背景

我被指派制作一个与Google地图显示找到结果的底页的行为类似的用户界面.

它有三个不同的阶段:

  1. 底部内容.上部区域仍然是可触摸的,不会在底部滚动任何东西
  2. 全屏内容,而上部区域有一个大标题.
  3. 全屏内容,而上部区域只有工具栏.

以下是我在Google地图上讨论的内容:

在此输入图像描述

问题

事实是,底页不是设计库的一部分(尽管这是请求的,这里).

不仅如此,UI似乎相当复杂,需要在多个阶段处理工具栏.

我试过的

我已经找到了底部片(一个很好的(足够的)库在这里),并添加内容到它的碎片样本,有大致相同的看法材料设计的样品,如图(如这里),有一个CollapsingToolbarLayout会照顾阶段2 + 3.

在我正在制作的应用程序中,我还必须在滚动时移动一个图标,但我认为如果我成功完成其余部分,这应该很容易.这是代码:

fragment_my.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"

        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/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>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/card_margin">

                <LinearLayout
                    style="@style/Widget.CardContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Info"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title"/> …
Run Code Online (Sandbox Code Playgroud)

android material-design android-design-library android-coordinatorlayout android-collapsingtoolbarlayout

106
推荐指数
2
解决办法
3万
查看次数

什么是CoordinatorLayout?

刚看了一下新的Android支持设计库的演示应用程序.它由Chris Banes在github上提供.通过应用程序,CoordinatorLayout使用沉重.此外,许多支持设计库类如FloatingActionButton,SnackBar,AppBarLayout等行为有所内使用时CoordinatorLayout.

有人可以CoordinatorLayout说明一下它是什么以及它与ViewGroupandroid中的其他s 有何不同,或者至少提供正确的学习途径CoordinatorLayout.

android android-support-library android-design-library android-coordinatorlayout

89
推荐指数
5
解决办法
6万
查看次数

无法在当前主题中找到样式'coordinatorLayoutStyle'

我正在使用最新版本的android studio(3.0),以及最新的构建工具(27)和类似的API级别.

布局不会在设计选项卡中呈现,这会导致很多麻烦,特别是我正在使用协调器布局.

我该如何解决这个问题?

android android-layout android-studio android-coordinatorlayout

65
推荐指数
7
解决办法
7万
查看次数

虽然RecyclerView没有足够的内容可以滚动,但AppBarLayout中的工具栏是可滚动的

AppBarLayout中的工具栏是否真的可以滚动,尽管带有"appbar_scrolling_view_behavior"的主容器没有足够的内容来真正滚动?

到目前为止我测试的内容:
当我使用NestedScrollView(带有"wrap_content"属性)作为主容器而TextView作为子容器时,AppBarLayout正常工作并且不滚动.

但是,当我使用仅包含少量条目的RecyclerView和"wrap_content"属性(因此无需滚动)时,即使RecyclerView从未收到滚动事件(使用OnScrollChangeListener测试),AppBarLayout中的工具栏也是可滚动的).

这是我的布局代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/ToolbarStyle" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

具有以下效果,工具栏可滚动,虽然没有必要:

我还通过检查所有RecyclerView项目是否可见并使用RecyclerView的setNestedScrollingEnabled()方法找到了解决此问题的方法.
尽管如此,它似乎更像是我想要的错误.任何意见?:d

编辑#1:

对于可能对我当前的解决方案感兴趣的人,我不得不将setNestedScrollingEnabled()逻辑放在Handler的postDelayed()方法中,延迟时间为5毫秒,因为LayoutManager在调用方法时总是返回-1第一个和最后一个项目是否可见.
我在onStart()方法中使用此代码(在我的RecyclerView初始化之后)以及每次发生RecyclerView内容更改后.

final LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        //no items in the RecyclerView
        if (mRecyclerView.getAdapter().getItemCount() == 0)
            mRecyclerView.setNestedScrollingEnabled(false);
        //if the first and the last item is visible
        else if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0
                && layoutManager.findLastCompletelyVisibleItemPosition() == mRecyclerView.getAdapter().getItemCount() …
Run Code Online (Sandbox Code Playgroud)

android android-xml android-coordinatorlayout android-appbarlayout

64
推荐指数
3
解决办法
2万
查看次数

CoordinatorLayout + AppBarLayout + NavigationDrawer

CoordinatorLayout与a AppBarLayout和a 组合时,我有一个布局问题NavigationDrawer.

问题是,NavigationDrawer及其内容隐藏在工具栏后面.我已经做了很多研究并尝试了很多重组,但没有一个"解决方案"解决了我的问题.

可以在这个小小的Webm视频中找到演示:https://www.dropbox.com/s/i5zfc2x2ts2fws7/navigation_drawer_stackoverflow32523188.webm ? dl =0

基本风格是Theme.AppCompat.Light.NoActionBar.

我的activity_overview.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:id="@+id/overview_coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimaryDark"
            app:layout_scrollFlags="enterAlways|scroll" />


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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusableInTouchMode="true">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/lorem_ipsum_long" />
        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.NavigationView
            android:id="@+id/nvView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/navigationdrawer_main" />

    </android.support.v4.widget.DrawerLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/overview_floating_action_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"

        android:clickable="true"
        android:src="@drawable/ic_add"
        app:layout_anchor="@id/overview_coordinator_layout"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior="@string/fab_onscroll_behavior" />

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

android android-layout android-coordinatorlayout android-appbarlayout android-drawer

59
推荐指数
2
解决办法
3万
查看次数

Android布局:具有滚动行为的Viewpager内的垂直Recyclerview内的水平Recyclerview

这是我正在尝试使用以下所有元素构建的应用程序:

蔬菜应用程序

但是,一切正常,我希望内部水平回收视图不捕获任何垂直滚动.所有垂直滚动必须朝向外部垂直回收视图,而不是水平回滚视图,因此垂直滚动将允许工具栏根据其scrollFlag退出视图.

当我将手指放在recyclerview的"StrawBerry Plant"部分并向上滚动时,它会滚出工具栏:

草莓植物

如果我将手指放在水平滚动视图上并向上滚动,它根本不会滚出工具栏.

以下是我目前为止的xml布局代码.

活动的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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container"
    android:clipChildren="false">

    <android.support.design.widget.CoordinatorLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container"
        >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            style="@style/CustomTabLayout"
            />

    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

"水果"片段 XML布局(其是用于该片段编码-片段被标记在上面图片):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:visibility="gone"
        android:layout_centerInParent="true"
        android:indeterminate="true"/>

<!--    <android.support.v7.widget.RecyclerView-->
    <com.example.simon.customshapes.VerticallyScrollRecyclerView
        android:id="@+id/main_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我使用了一个名为VerticallyScrollRecyclerView的自定义类,该类跟随google在视图组中处理触摸事件的示例.它的目的是拦截和使用所有垂直滚动事件,以便它可以滚动/滚出工具栏: …

android android-layout android-recyclerview android-coordinatorlayout android-appbarlayout

59
推荐指数
3
解决办法
3万
查看次数

如何以编程方式为工具栏设置app:layout_scrollFlags

我的应用程序有一个viewpager和4个选项卡,每个选项卡都有许多片段.但我只想在2个特定选项卡中滚动recyclerview时向上/向下滚动工具栏.但我不知道如何阻止工具栏滚动其他选项卡.我试图为每个片段导入工具栏,但似乎我不能这样做.有人有想法解决这个问题吗?

android android-fragments android-toolbar android-coordinatorlayout

57
推荐指数
2
解决办法
3万
查看次数