【问题标题】:How do I correctly implement a Snackbar, Coordinator Layout and a bottom|right FAB?如何正确实现 Snackbar、协调器布局和底部|右 FAB?
【发布时间】:2016-03-09 17:06:21
【问题描述】:

我目前正在使用设计支持库。我已经尝试用我的 CoordinatorLayout 来实现 Snackbar 来包裹整个布局。当要显示 Snackbar 时,它会提高 FAB。但它没有努力下降,因此保持在转换距离之上。

但是,当我通过滑动关闭 Snackbar 时,它确实会下降。所以 CoordinatorLayout 知道 FAB 包裹在里面。

.xml

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

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary"
            android:elevation="4dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/ad_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/toolbar"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/ad_view">

            <include layout="@layout/card_view" />

        </ScrollView>

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btn_generate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_refresh_white_24dp"
        app:elevation="6dp"
        app:fabSize="normal"
        app:pressedTranslationZ="12dp" />

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

.java

Snackbar.make(mCoordinatorLayout, R.string.copied_to_clipboard,
                Snackbar.LENGTH_SHORT).show(); 

【问题讨论】:

    标签: android android-coordinatorlayout floating-action-button android-snackbar


    【解决方案1】:

    您可以尝试下面的代码,它应该可以正常工作。这就是我在所有项目中实现我的快餐栏的方式,它从来没有让我失望过。究;快乐的编码 ;-) !

    Snackbar.make(findViewById(android.R.id.content),"Your text",Snackbar.LENGTH_SHORT).show();
    

    记住,永远不要忘记你的show() 方法,没有它你的snackbar 就毫无用处。

    【讨论】:

    • 关键是使用 android.R.id.content 而不是一些不存在的 ID。为您节省很多压力并保持理智。
    【解决方案2】:

    如果你想显示Snackbar,这样当它出现时,CoordinatorLayout 会被调整,然后试试下面的代码

    findViewById(R.id.btn_generate).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            Snackbar.make(view, "Hello Snackbar", Snackbar.LENGTH_LONG).show();
          }
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2017-12-04
    • 2023-03-06
    相关资源
    最近更新 更多