用拇指对齐Android seekbar

问题描述:

我试图将seekbar对齐到视图的顶部,但我无法用拇指居中。 是否有某种“alignCenter”与RelativeLayout子项。用拇指对齐Android seekbar

这里是我的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" /> 

例如,谷歌播放音乐做的。

You can see the orange seekbar is well align with the top of the control view

+0

你能更具体吗? – 2014-10-01 21:20:03

+0

画图可能吗? – danny117 2014-10-01 21:26:02

+0

我已经添加了一张图片,您可以在正确的图片中看到它,该图片栏与该视图(由拇指的中心)对齐,并位于控制器视图的顶部 – Tsunaze 2014-10-01 21:36:29

正如你所说,你的seekbar的高度可能会有所不同,除非你指定它不同。这是你如何确保它始终适合所有人。

android:layout_above="@id/rl_fragment_audio_player" 

将您的seekbar设置在rl_fragment_audio_player的上方。没有直接的方法来指定centerAlignWith,但我会删除它在其父项中占据的空间。要知道搜索栏的高度,您必须等到全局布局发生变化。此代码应放置在您的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" /> 
+0

谢谢,它现在的工作,快速的问题,seekbar从来没有开始或结束在屏幕的两侧,你知道我怎么能解决这个问题? – Tsunaze 2014-10-07 13:54:43

+0

如果未指定,则SeekBar将启动填充。如果您在seekbars xml中添加android:padding =“0dp”,它将被解决。不要试图以编程方式进行,这会导致错误的进度。 – 2014-10-07 14:38:58

+0

是的,非常感谢,它的作品完全像Google Music Now!谢谢 – Tsunaze 2014-10-07 15:34:05

您是否尝试过加入负余量搜索条?

<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" /> 
+0

是的,我有,它有点工作,但有没有一种好办法呢?因为我不想选择任意数量的dp来确定。或者它总是与dp数量相同? (PS:我选择-16dp) – Tsunaze 2014-10-07 11:37:57

+0

我会做的是提供我自己的拇指资源,以便我可以控制身高,而且我不需要担心不同的设备。顺便说一句,我同意这不是一个很好的方式来做到这一点 – Malvin 2014-10-08 02:43:36