对准拇指Android的搜索栏 [英] Align Android seekbar with thumb

查看:222
本文介绍了对准拇指Android的搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调整SeekBar以一个视图的顶部,但我不能用拇指居中。
是否有某种alignCenter与RelativeLayout的孩子。

I'm trying to align the seekbar to the top of a view, but i can't center it with the thumb. Is there some kind of "alignCenter" with the RelativeLayout children.

下面是我的XML code的样本:

Here's a sample of my xml code :

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

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

For example, Google Play Music does it.

推荐答案

正如你说你的搜索栏的高度可能会有所不同,除非你指定它的不同。这里是你如何能确保它适合所有的,总是这样。

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"

在设置上述rl_fragment_audio_player顶部的搜索栏底部。还有就是要指定centerAlignWith没有直接的方法,但我会删除其占用范围内它的父的空间。要知道,搜索栏的高度,你必须等到全球布局发生了变化。这code应该放在你的onCreateView在

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
                }

            });

在XML中的最后一个视图将在前面。因此,要确保你的搜索栏是其他视图后加入。像这样

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

这篇关于对准拇指Android的搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆