繁体   English   中英

用拇指对齐Android搜索栏

[英]Align Android seekbar with thumb

我正在尝试将搜索栏对齐到视图的顶部,但我无法用拇指将其居中。 是否有某种与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音乐会这样做。

您可以看到橙色搜索栏与控件视图的顶部对齐

如你所说,你的搜索栏的高度可能会有所不同,除非你指定它不同。 以下是如何确保它始终适合所有情况。

android:layout_above="@id/rl_fragment_audio_player"

将你的搜索栏设置在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中的最后一个视图将位于前面。 因此,请确保在其他视图之后添加搜索条。 像这样

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM