简体   繁体   English

Android:如何以编程方式设置浮动操作按钮的重力?

[英]Android : How to set gravity of Floating Action Button Programmatically?

I want to move the position of Floating action button programmatically at a certain place in some fragment because sometimes the floating action is covering the important contents of that fragment.我想以编程方式将浮动操作按钮的位置移动到某个片段中的某个位置,因为有时浮动操作会覆盖该片段的重要内容。 I got the reference of floating action button in the activity and stored it in a static variable.I am trying to change its gravity in the onResume method of the fragment.我在活动中获得了浮动操作按钮的引用并将其存储在一个静态变量中。我试图在片段的 onResume 方法中更改其重力。 As you can see in the image that fab is covering y-axis so I want to move it to the END.正如您在图像中看到的,fab 覆盖了 y 轴,所以我想将它移动到 END。 Image for the reference .图片供参考

The code in the onResume method of the fragment(not working):片段的 onResume 方法中的代码(不工作):

override fun onResume() {
    super.onResume()
    val params : CoordinatorLayout.LayoutParams = BaseActivity.fab!!.layoutParams as CoordinatorLayout.LayoutParams
    params.anchorGravity = GravityCompat.END
    BaseActivity.fab!!.layoutParams = params
}

XML file of main activity主要活动的 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.mandar.hackerthon_app.Activities.BaseActivity"
    tools:showIn="@layout/app_bar_base">

    <FrameLayout
        android:id="@+id/frameBaseFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_margin="20dp"
        android:scaleType="center"
        android:elevation="10dp"
        app:backgroundTint="@color/common_google_signin_btn_text_dark_default"
        app:srcCompat="@mipmap/ic_connection_on_round" />

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

In your XML file, add the app:layout_anchor property for FloatingActionButton , as layout_anchorGravity is only taken into account if you specify an anchor view.在您的 XML 文件中,为FloatingActionButton添加app:layout_anchor属性,因为只有在指定锚视图时才会考虑layout_anchorGravity

In your case, this anchor may be @id/frameBaseFragment .在你的情况下,这个锚点可能是@id/frameBaseFragment Note that if you want to move FloatingActionButton to the bottom-end of its CoordinatorLayout parent, you may instead change the value of layoutGravity .请注意,如果您想将FloatingActionButton移动到其CoordinatorLayout父级的底端,您可以改为更改layoutGravity的值。

Also, it is strongly discouraged to store references to Views in static variables as it prevents the associated Activity from being garbage-collected, causing huge memory leaks.此外,强烈建议不要在静态变量中存储对视图的引用,因为它会阻止相关联的Activity被垃圾收集,从而导致巨大的内存泄漏。 This might also be the cause of your problem.这也可能是您的问题的原因。

fab does not have a gravity property but other layouts do. fab没有gravity属性,但其他布局有。 So an approach I found was to wrap the fab I have in another layout:所以我发现的一种方法是将我拥有的fab包装在另一个布局中:

// ... FloatingActionButton fab defined somewhere ...

val fabHolder = LinearLayout(this.context)  // layout type irrelevant
fabHolder.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
fabHolder.gravity = Gravity.END or Gravity.BOTTOM  // bottom-right
fabHolder.addView(fab)

relView.addView(fabHolder)

This replaces the following line of code:这将替换以下代码行:

relView.addView(fab)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM