【问题标题】:How to increase the size of the floating action button according to screen sizes如何根据屏幕大小增加浮动操作按钮的大小
【发布时间】:2018-01-20 19:42:17
【问题描述】:

我正在开发一个需要实现浮动操作按钮的应用程序。另外,我需要根据屏幕大小增加或减小按钮的大小。

下面是我的浮动操作按钮代码:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:fabSize="normal"
        app:backgroundTint="#2e2745"
        android:tint="#ffffff"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        app:srcCompat="@android:drawable/ic_input_add" />

我尝试从android:layout_height="wrap_content"android:layout_width="wrap_content" 正常更改大小,但根本没有反映。

6 英寸以上的屏幕尺寸,浮动按钮看起来非常小,我不知道如何根据屏幕尺寸更改尺寸。

我在这里做错了吗??

我们将不胜感激。

提前致谢。

【问题讨论】:

    标签: android xml android-layout android-fragments


    【解决方案1】:
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    

    在引入 getSize 之前(在 API 级别 13 中),您可以使用现已弃用的 getWidth 和 getHeight 方法:

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();  // deprecated
    int height = display.getHeight();  // deprecated
    

    现在你有了可以在语法上设置宽度的显示尺寸

    FloatingActionButton btn=(FloatingActionButton)findViewById(R.id.fab);
    btn.setLayoutParams(new LinearLayout.LayoutParams(width/20, height/30)); //as you want
    

    【讨论】:

    • 请在我的代码中替换新的 LinearLayout.LayoutParams,它是包含 FloatingButton 的 LinearLayout,我认为你放入了 RelativeLayout,所以使用新的 RelativeLayout.LayoutParams()
    • 别忘了展示你所有的 xml 代码 :) 亲自查看并为你检查
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多