Align Android seekbar with thumb

后端 未结 2 1326
小蘑菇
小蘑菇 2021-01-20 04:22

I\'m trying to align the seekbar to the top of a view, but i can\'t center it with the thumb. Is there some kind of \"alignCenter\" with the RelativeLayout children.

2条回答
  •  失恋的感觉
    2021-01-20 05:01

    As you said the height of your seekbar might vary, unless you specify it different. Here is how you can make sure it fits all, always.

    android:layout_above="@id/rl_fragment_audio_player"
    

    is setting your seekbar bottom above rl_fragment_audio_player top. There is no direct method to specify centerAlignWith but I would remove the space it occupies within it's parent. To know the height of the seekbar you must wait until the global layout has changed. This code should be placed inside your onCreateView

    sb = (SeekBar) rootView.findViewById(R.id.sb_fragment_audio);
        sb.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener(){
    
                    @Override
                    public void onGlobalLayout() {
    
                        sb.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    
                        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sb.getLayoutParams();
                        params.setMargins(0, - sb.getHeight() / 2, 0, - sb.getHeight() / 2);
                        sb.setLayoutParams(params);
                        // I suggest you set the seekbar visible after this so that it won't jump
                    }
    
                });
    

    The last view in the xml will be in the front. Therefore make sure your seekbar is added after the the other views. Like this

    
    
        // your bottom panel
    
    
    
    
    
    
    

提交回复
热议问题