Search code examples
androidviewpositionmarginbadge

The github android library, Viewbadge, How to set rightMargin position?


I tried this, but doesnot work:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,      
            LayoutParams.WRAP_CONTENT
    );

   params.setMargins(0, 0, 200, 0);
   badge.setLayoutParams(params);

   badge.show();

Could anyone give some surely runnable code? Thanks a lot!


Solution

  • Fortunately, I'v gotten the reason why the layoutParams doesnot work. After scanning the library code , I find it do set layoutparams in applyLayoutParams method from show method :

    private void applyLayoutParams() {
    
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
        switch (badgePosition) {
        case POSITION_TOP_LEFT:
            lp.gravity = Gravity.LEFT | Gravity.TOP;
            lp.setMargins(badgeMarginH, badgeMarginV, 0, 0);
            break;
        case POSITION_TOP_RIGHT:
    .......
    

    So, our marigin setting is overriden.

    And then, I find a cool solution. Just using setBadgeMargin method is OK before show method. Because it will change the badgeMarginH and badgeMarginV which is applied in show method. By it, we can change the position of badge freely and dynamically.

    haha.... Hope helpful for others