2014-10-01 197 views
2

我試圖將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

回答

2

正如你所說,你的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

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

是的,我有,它有點工作,但有沒有一種好辦法呢?因爲我不想選擇任意數量的dp來確定。或者它總是與dp數量相同? (PS:我選擇-16dp) – Tsunaze 2014-10-07 11:37:57

+0

我會做的是提供我自己的拇指資源,以便我可以控制身高,而且我不需要擔心不同的設備。順便說一句,我同意這不是一個很好的方式來做到這一點 – Malvin 2014-10-08 02:43:36

相關問題