用拇指对齐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" />
例如,谷歌播放音乐做的。
答
正如你所说,你的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" />
答
您是否尝试过加入负余量搜索条?
<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" />
你能更具体吗? – 2014-10-01 21:20:03
画图可能吗? – danny117 2014-10-01 21:26:02
我已经添加了一张图片,您可以在正确的图片中看到它,该图片栏与该视图(由拇指的中心)对齐,并位于控制器视图的顶部 – Tsunaze 2014-10-01 21:36:29