将 Android seekbar 与滑块对齐

3

我正在尝试将进度条与视图顶部对齐,但是我无法使用滑块使其居中。 RelativeLayout子元素中是否有某种“alignCenter”功能。

以下是我的xml代码示例:

<RelativeLayout
    android:id="@+id/rl_fragment_audio_player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/black_transparent"
    android:padding="5dp"
    android:visibility="gone" >

    <TextView
        android:id="@+id/tv_fragment_audio_begin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/tv_fragment_audio_end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:textColor="@color/white" />

    <Button
        android:id="@+id/btn_fragment_audio_play"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"
        android:background="@drawable/btn_play"
        android:visibility="gone" />
</RelativeLayout>

<SeekBar
    android:id="@+id/sb_fragment_audio"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_above="@id/rl_fragment_audio_player" />

例如,Google Play Music 就是这样做的。 您可以看到橙色进度条与控制视图顶部对齐

你能说得更具体一些吗? - Mark Buikema
我已经添加了一张图片,您可以在右侧看到它,拖动条与视图(通过拇指的中心)对齐到控制器视图的顶部。 - Tsunaze
@Tsunaze 很棒的图片。 - danny117
我刚刚编辑了帖子,这样你就可以看到我的代码的一部分了。 - Tsunaze
您是否尝试将SeekBar放在下面的RelativeLayout之上,以便拇指覆盖它(同时还会覆盖位于SeekBar上方的其他内容)? - user
显示剩余5条评论
2个回答

2

正如您所说,您的seekbar的高度可能会有所不同,除非您指定了不同的高度。以下是您可以确保它始终适合所有内容的方法。

android:layout_above="@id/rl_fragment_audio_player"

将SeekBar设置在rl_fragment_audio_player顶部之上。目前没有直接指定centerAlignWith的方法,但可以移除其在父布局中占用的空间。要知道SeekBar的高度,需要等待全局布局改变后才能计算。以下代码应放置在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
                }

            });

在xml中最后一个视图将会被放在前面,因此请确保您的SeekBar在其他视图之后添加。示例如下:
<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" />

谢谢,现在它可以工作了。我有一个快速的问题:SeekBar从来没有开始或结束在屏幕的两侧,你知道我怎么解决这个问题吗? - Tsunaze
是的,如果没有指定,SeekBar会初始化一个填充。如果在SeekBar的xml中添加android:padding="0dp",问题就会得到解决。不要试图通过编程来解决它,否则会导致进度条位置错误。 - Adam Wigren
非常感谢,它的工作方式与Google音乐现在完全相同!谢谢。 - Tsunaze

0
你尝试过给进度条添加负边距吗?
<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" />

是的,我做到了,它有点起作用,但有没有更好的方法来做到这一点?因为我不想随意选择一个dp数来合并。或者总是相同的dp数吗?(PS:我选择了-16dp) - Tsunaze
我会提供自己的缩略图资源,这样我就可以控制高度,而不必担心不同的设备。顺便说一句,我同意这不是一个真正好的方法。 - Malvin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接