Align Android seekbar with thumb

浪子不回头ぞ 提交于 2019-12-01 21:28:11

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

<RelativeLayout
    android:id="@+id/rl_fragment_audio_player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    // your bottom panel

</RelativeLayout>

<View
    android:id="@+id/image_above"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/rl_fragment_audio_player" />

<SeekBar
    android:id="@+id/sb_fragment_audio"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/image_above" />

Have you tried adding a negative margin to the seekbar?

<SeekBar
    android:id="@+id/sb_fragment_audio"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_marginBottom="-15dp"
    android:layout_above="@id/rl_fragment_audio_player" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!