我在一个 Activity 中有一个 float 操作按钮,我计划在 fragment 上显示它。当波纹可见时,FAB 不会响应点击。 我搜索了类似的问题,但我找到的答案不起作用。
这是我的 FAB 布局。
<android.support.design.widget.CoordinatorLayout 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"
app:theme="@style/MyTheme">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainActivityFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/homeFAB"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="bottom|center"
android:layout_margin="16dp"
android:src="@drawable/ic_add_image"
android:onClick="runThis"
app:layout_anchor="@id/MainActivityFragmentContainer"
app:layout_anchorGravity="bottom|center" />
我重新排列了协调器布局内布局中按钮的顺序,但不起作用。
然后我将 android:onclick="runThis"
添加到 XML 中的 FAB 和 Activity 中的方法。
public void runThis(View view) {
Toast.makeText(SlidingMenuActivity.this, "CLICKED", Toast.LENGTH_LONG).show();
}
出现以下错误:
java.lang.IllegalArgumentException: Expected receiver of type xxx.xxx.SlidingMenuActivity, but got android.support.v7.internal.view.ContextThemeWrapper
请帮我弄清楚我缺少什么。 谢谢
最佳答案
您正在使用 fragment ,您应该使用单击监听器,而不是像这样在 xml 上定义 onClick... 在 onCreate View 上充气 fab...
FloatingActionButton myFab = (FloatingActionButton) myView.findViewById(R.id.homeFAB);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
doyourThing();
}
});
或者 将 SlidingActivity.this 替换为 getActivity()
关于android - float 操作按钮不响应单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40803629/