【问题标题】:Floating action button doesn't appear in kitkat浮动操作按钮不会出现在 kitkat 中
【发布时间】:2016-04-27 07:28:44
【问题描述】:

我创建了一个单页应用程序,其中浮动操作按钮用于共享页面中的内容。它在 android lollipop 中完美运行,但是当我在 kitkat 上运行该应用程序时,它没有显示。由于唯一的交互式组件是 FAB,因此用户只能盯着看。我进行了一些搜索,发现 FAB 应该在 kitkat 上完美运行,并且只有在棒棒糖(如填充)方面存在一些问题。但是,它在棒棒糖中工作得很好。对于比棒棒糖更早的版本,我是否必须将其更改为按钮?我在这里的第一个问题。任何帮助表示赞赏。谢谢。

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.ataraxianstudios.decksharer.MainActivity"
    >


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/share1"/>

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#016cb4">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/llMain"
        >
<Spinner
        android:id="@+id/osversions"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        />
    <ImageView
        android:layout_width="60dp"
        android:layout_height="80dp"
        android:id="@+id/selectedImage"
        />

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

【问题讨论】:

标签: android xml android-5.0-lollipop android-4.4-kitkat floating-action-button


【解决方案1】:

我也有同样的问题,可能是由于材料设计和 kitkat 之后引入的材料设计,所以在某些选项卡或布局上存在一些问题,但试试这个它对我有用,也许对你有用..

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ataraxianstudios.decksharer.MainActivity"
>



//Replace your fab from here

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#016cb4">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/llMain"
    >
<Spinner
    android:id="@+id/osversions"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    />
<ImageView
    android:layout_width="60dp"
    android:layout_height="80dp"
    android:id="@+id/selectedImage"
    />

</LinearLayout>
</ScrollView>


//to here:
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/share1"/>
</android.support.design.widget.CoordinatorLayout>

【讨论】:

    【解决方案2】:

    您是否在使用设计支持库? 这是一个例子:

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add" />
    

    在你的 build.gradle 中:

    依赖{ ...... 编译'com.android.support:design:xx.yy.zz' }

    最后,在您的 Activity 中,您可以像这样引用 FAB:

    FloatingActionButton btn = (FloatingActionButton) findViewById(R.id.fab);
    

    希望对你有帮助!!

    【讨论】:

    • 我就是这么做的。但是,当我在运行 4.4 的设备上测试它时,它没有显示。
    • 你能展示你在布局中使用的代码吗?
    • 我阅读了您分享的帖子。这是否意味着我必须检查 api 版本然后使用浮动操作按钮?那么,对于 kitkat,我可能使用按钮而不是 FAB 来达到相同的效果?
    • 试试这个教程进行材料设计......truiton.com/2015/02/…
    【解决方案3】:

    为你的 FloatingActionButton 使用 bringToFront

    fab = view.findViewById(R.id.fab_hours);
    fab.bringToFront();
    

    【讨论】:

      猜你喜欢
      • 2015-01-11
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多