android - FloatingActionButton 不能正常工作

标签 android android-4.4-kitkat floating-action-button android-5.1.1-lollipop

我在使用 FloatingActionButton 时遇到了一些问题,它在模拟器上可以正常工作,但是当我安装到我的手机(Galaxy s4,Android V4.4.4)时它无法正常工作我附上了一些图片来说明错误。谁能告诉我我能做什么?

我把 View 放在 fragment 中

fragment 类

public class MiniGameMenuFragment extends Fragment implements View.OnClickListener {
    protected FloatingActionButton more_menu_btn, card_view_mode_btn, expand_view_btn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mini_menu_fragment, container, false);
        this.more_menu_btn = (FloatingActionButton) view.findViewById(R.id.more_c_btn);
        this.card_view_mode_btn = (FloatingActionButton) view.findViewById(R.id.card_view_mode_c_btn);
        this.expand_view_btn = (FloatingActionButton) view.findViewById(R.id.expand_view_c_btn);

        this.more_menu_btn.setOnClickListener(this);
        this.card_view_mode_btn.setOnClickListener(this);
        this.expand_view_btn.setOnClickListener(this);

        return view;
    }


    @Override
    public void onClick(View v) {

    }

}

迷你菜单 fragment .xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/red">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/more_c_btn"
            android:layout_width="38dp"
            android:layout_height="38dp"
            android:clickable="true"
            android:scaleType="center"
            android:src="@mipmap/ic_more_vert_white_24dp"
            app:backgroundTint="#ff9800"
            app:borderWidth="0dp"
            app:elevation="8dp"
            app:fabSize="normal"
            app:pressedTranslationZ="12dp"
            app:rippleColor="#ff5722" />
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/card_view_mode_c_btn"
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:clickable="true"
        android:src="@mipmap/card_expanded_view_mode"
        app:backgroundTint="#ff9800"
        app:borderWidth="0dp"
        app:elevation="8dp"
        app:pressedTranslationZ="12dp"
        app:rippleColor="#ff5722" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/expand_view_c_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:clickable="true"
        android:src="@mipmap/zoom_out_white"
        app:backgroundTint="#ff9800"
        app:borderWidth="0dp"
        app:elevation="8dp"
        app:fabSize="normal"
        app:pressedTranslationZ="12dp"
        app:rippleColor="#ff5722" />
</LinearLayout>

主要 Activity xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gameBackground"
    android:orientation="vertical"
    tools:context="com.example.MainActivity">


    <RelativeLayout
        android:id="@+id/floating_menu_container_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true">

    </RelativeLayout>

</RelativeLayout>

主要 Activity 类

public class MainActivity extends AppCompatActivity {
    FragmentManager fragmentManager;
    FragmentTransaction transaction;

    MiniGameMenuFragment miniGameMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        this.miniGameMenu = new MiniGameMenuFragment();

        this.fragmentManager = getFragmentManager();
        this.transaction = fragmentManager.beginTransaction();

        this.transaction.add(R.id.floating_menu_container_layout, miniGameMenu, "miniGameMenuFragment");
        this.transaction.commit();
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
    }
}

在模拟器上

enter image description here

在 Galaxy 4s 手机上

enter image description here

已解决:

问题是高度,因为@SadiqMdAsif 说 8dp 对于我的 galaxy s4 分辨率来说太多了,这就是为什么它创建了一个额外的边距,所以我减少到 2dp

最佳答案

在设计器页面中从此选项创建变体

enter image description here

关于android - FloatingActionButton 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41561046/

相关文章:

android - onListItemClick 不起作用

android - 如何在 android kitkat 中保持服务活跃?

android - 在运行 4.3/pre L 的 Android 上找不到类/NoClassDefFound 错误

android - 如何以编程方式删除 fab 的布局 anchor_property?

android - sqlite数据库排序

android - 在 Android 手机主屏幕上检测触摸或滑动

android - 带有 Gson 的 Ormlite ForeignCollection

Android Alarm Manager 与 4.4.x 不一致。支持 minSDKVersion 9 和 targetSdkVersion 21 的替代解决方案是什么

android - Android float 操作按钮菜单项上的 Espresso UI 测试

android - 如何隐藏键盘上的 float 操作按钮?