【问题标题】:FAB between layouts布局之间的 FAB
【发布时间】:2017-05-02 02:45:43
【问题描述】:

这是我的 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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

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

    <LinearLayout
        android:id="@+id/layout_first"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.8"
        android:background="@color/ColorPrimary"
        android:orientation="horizontal" />

    <LinearLayout
        android:id="@+id/layout_second"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.4"
        android:background="@color/ColorPrimaryLight"
        android:orientation="horizontal" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:clickable="true"
        android:src="@drawable/ic_search_black_48dp"
        app:backgroundTint="#df0909"
        app:elevation="4dp"
        app:layout_anchor="@id/layout_first"
        app:layout_anchorGravity="bottom|right|end" />

    </LinearLayout>

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

这就是它的外观:

我从这个答案尝试过:How can I add the new “Floating Action Button” between two widgets/layouts

但是当我将 compile 'com.android.support:design:23.4.0' 更改为 compile 'com.android.support:design:25.0.1' 时,我收到了一个错误:

apply plugin: 'com.android.application'
android {
 compileSdkVersion 23
 buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.test.test"
    minSdkVersion 21
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
} 
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

那么有没有办法实现这样的外观,其中工厂位于两个布局之间(不需要滚动):

【问题讨论】:

  • 您在LinearLayout 中有FloatingActionButton。您希望它作为CoordinatorLayout 的直接子代。即,将其移至结束 LinearLayout 标记之后。
  • @MikeM。谢谢你。做到了。你为什么不发布答案?

标签: android android-layout material-design floating-action-button


【解决方案1】:

您的FloatingActionButton 位于LinearLayout 中。要使layout_anchor* 属性起作用,它必须是CoordinatorLayout 的直接子级。

只需将FloatingActionButton 元素移动到结束LinearLayout 标记之后。

【讨论】:

    【解决方案2】:

    你可以使用下一个:

    <FloatingButton
     app:layout_anchor="@id/layout_first"
        app:layout_anchorGravity="bottom|left|end"/>
    

    在你想要的布局上使用它,并尝试用谷歌搜索它以获得所需的效果。锚定的教程很多,所以查一下。但是,重点是您需要将其锚定到您想要的布局上。

    【讨论】:

      【解决方案3】:
      android:layout_margin="-20dp"
      

      使用负边距(FAB 高度的一半)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-09
        • 2016-11-21
        • 2016-11-14
        • 2021-12-11
        • 2016-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多