【问题标题】:FloatingActionButton strange press behaviorFloatingActionButton 奇怪的按下行为
【发布时间】:2016-04-18 23:00:58
【问题描述】:

我不知道是什么突然导致了这种情况,因为我认为我没有更改任何代码(也许这是 Android Studio 2.0 的错误?)。

但是现在,当我按下 FloatingActionButton 时,它会一直保持按下状态,并且在我第一次单击它时不会执行任何操作。如果我再次开始单击它,它似乎可以工作(但看起来仍然被按下)。

在我的 OnCreate 方法中:

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    assert fab != null;

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           //does stuff here, behavior is the same even if I remove this code
        }
    });

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



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

        <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"
            android:baselineAlignBottom="false"
            android:focusableInTouchMode="true"
            />

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

</LinearLayout>

【问题讨论】:

  • 为什么你有assert fab != null;
  • 只是为了摆脱 Android Studio 给我的警告;可能不需要

标签: android android-studio android-activity android-xml floating-action-button


【解决方案1】:

从您的 FAB 中删除 android:focusableInTouchMode="true"

<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"
    android:baselineAlignBottom="false"
    />

【讨论】:

  • 解决了!我想知道为什么我首先添加了它,或者为什么直到现在它才决定开始表现得有趣。
猜你喜欢
  • 2017-07-15
  • 1970-01-01
  • 2011-02-26
  • 2011-11-05
  • 1970-01-01
  • 2016-05-19
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多