Android P visibilityawareimagebutton.setVisibility can only be called from the same library group

前端 未结 8 1830
不思量自难忘°
不思量自难忘° 2020-12-23 20:00

I\'m trying to use the new Android P FloatingActionButton that\'s part of the com.google.android.material.floatingactionbutton.FloatingActionButton and I\'m get

相关标签:
8条回答
  • 2020-12-23 20:28

    Using Method 1

    demoFab.show(); // in place of visible
    demoFab.hide(); // in place of Invisible suppress the warning/error for me.
    

    and Method 2

    @SuppressLint("RestrictedApi") // also suppressed the warning
    private void setUp() {
        ....
    }
    

    update:

    Method 3:

    demoFab.setVisibility(View.GONE);
    demoFab.setVisibility(View.INVISIBLE);
    demoFab.setVisibility(View.VISIBLE);
    

    Method 4:

    demoFab.visibility = View.GONE
    demoFab.visibility = View.INVISIBLE
    demoFab.visibility = View.VISIBLE
    
    0 讨论(0)
  • 2020-12-23 20:31

    For com.google.android.material.floatingactionbutton.FloatingActionButton

    Visibility

    Use the show and hide methods to animate the visibility of a FloatingActionButton. The show animation grows the widget and fades it in, while the hide animation shrinks the widget and fades it out.

    Source: https://material.io/develop/android/components/floating-action-button/

    For android.support.design.widget.FloatingActionButton use setVisibility() Method

    0 讨论(0)
  • 2020-12-23 20:35

    For Kotlin I have an extension method

    fun viewsVisibility(visibility: Int, vararg views: View) {
        for (view in views) { view.visibility = visibility }
    }
    

    Then in then in the code you can do the following

    viewsVisibility(View.VISIBLE, demoFab) 
    viewsVisibility(View.GONE, demoFab)
    viewsVisibility(View.INVISIBLE, demoFab, addFab, removeFab)
    

    The error will be gone and this gives the flexibility for any visibility state along with taking in a list of views to handle. There are a lot of times I need to handle more than one view at a time as shown in the final example line.

    0 讨论(0)
  • 2020-12-23 20:37

    For me above methods did not work so I created an extension function to make it work.

    fun View.showView() {
        this.visibility = View.VISIBLE
    }
    
    fun View.hideView() {
        this.visibility = View.GONE
    }
    

    now call like

    binding.fabAdded.showView()
    
    0 讨论(0)
  • 2020-12-23 20:38

    Seems to work fine just to cast it to a view.

    (mFloatingActionButton as View).visibility = INVISIBLE

    Of course you need to remember that the visibility may affect other components, so you should probably use show() and hide() at the same time to make sure other components are notified of the change.

    0 讨论(0)
  • 2020-12-23 20:46
    if(data){
                fragmentPendingApprovalDetailsBinding.fabPendingList.show();
            }else {
                fragmentPendingApprovalDetailsBinding.fabPendingList.hide();
            }
    
    0 讨论(0)
提交回复
热议问题