如何自定义搜索栏的拇指

发布于 2024-09-26 04:19:30 字数 192 浏览 7 评论 0原文

我想在 SeekBar 的拇指上方显示一个 TextView,它将显示搜索栏的当前进度。当我们向前和向后移动拇指时,TextView 也会随着拇指移动(TextView 也应该位于拇指上方)。

请提供任何线索,代码片段总是受欢迎的。

I want to display a TextView just above thumb of the SeekBar that will display the current progress of seek bar. As we move the thumb forward and backward the TextView will also move with thumb (the TextView should also be above the thumb).

Please provide any clue, code snippet are always welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白日梦 2024-10-03 04:19:30

通过将 TextViewpadding 设置为以下方式,可以更轻松地移动文本

int xPos = ((SeekbarInstance.getRight() - SeekbarInstance.getLeft()) * SeekbarInstance.getProgress()) / SeekbarInstance.getMax();
textViewInstance.setPadding(xPos, 0, 0, 0);

Moving the text can even more easily done by setting the padding for the TextView as:

int xPos = ((SeekbarInstance.getRight() - SeekbarInstance.getLeft()) * SeekbarInstance.getProgress()) / SeekbarInstance.getMax();
textViewInstance.setPadding(xPos, 0, 0, 0);
听你说爱我 2024-10-03 04:19:30

活动的布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <!-- Main context -->
    <LinearLayout android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <SeekBar android:id="@+id/skbSample" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_marginLeft="10dip" 
            android:layout_marginRight="10dip"
            android:max="35">
        </SeekBar>
    </LinearLayout>
        <!-- For display value -->
    <AbsoluteLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:text="0" 
            android:id="@+id/txvSeekBarValue" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:background="#FF777777"
            android:visibility = "invisible">
        </TextView>
    </AbsoluteLayout>
</FrameLayout>

在创建时初始化您的控件:

mTxvSeekBarValue = (TextView) this.findViewById(R.id.txvSeekBarValue);
mSkbSample = (SeekBar) this.findViewById(R.id.skbSample);
mSkbSample.setOnTouchListener(new OnTouchListener() {       
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            ShowSeekValue((int)event.getX(), mTxvSeekBarValue.getTop());
            mTxvSeekBarValue.setVisibility(View.VISIBLE);   
        }
        else if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            ShowSeekValue((int)event.getX(), mTxvSeekBarValue.getTop());
        }
        else if(event.getAction() == MotionEvent.ACTION_UP)
        {
            mTxvSeekBarValue.setVisibility(View.INVISIBLE);
        }
        return false;
    }
});

函数将移动您的值:

private void ShowSeekValue(int x, int y)
{
    if(x > 0 && x < mSkbSample.getWidth())
    {
        AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, x, y);
        mTxvSeekBarValue.setLayoutParams(lp);
    }
}

Layout of your activity

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <!-- Main context -->
    <LinearLayout android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <SeekBar android:id="@+id/skbSample" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_marginLeft="10dip" 
            android:layout_marginRight="10dip"
            android:max="35">
        </SeekBar>
    </LinearLayout>
        <!-- For display value -->
    <AbsoluteLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:text="0" 
            android:id="@+id/txvSeekBarValue" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:background="#FF777777"
            android:visibility = "invisible">
        </TextView>
    </AbsoluteLayout>
</FrameLayout>

Initialize your controls on create:

mTxvSeekBarValue = (TextView) this.findViewById(R.id.txvSeekBarValue);
mSkbSample = (SeekBar) this.findViewById(R.id.skbSample);
mSkbSample.setOnTouchListener(new OnTouchListener() {       
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            ShowSeekValue((int)event.getX(), mTxvSeekBarValue.getTop());
            mTxvSeekBarValue.setVisibility(View.VISIBLE);   
        }
        else if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            ShowSeekValue((int)event.getX(), mTxvSeekBarValue.getTop());
        }
        else if(event.getAction() == MotionEvent.ACTION_UP)
        {
            mTxvSeekBarValue.setVisibility(View.INVISIBLE);
        }
        return false;
    }
});

Function will move your value:

private void ShowSeekValue(int x, int y)
{
    if(x > 0 && x < mSkbSample.getWidth())
    {
        AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, x, y);
        mTxvSeekBarValue.setLayoutParams(lp);
    }
}
追风人 2024-10-03 04:19:30

找到了更好的方法。源自示例:

mTxvSeekBarValue = (TextView) this.findViewById(R.id.txvSeekBarValue);
mSkbSample = (SeekBar) this.findViewById(R.id.skbSample);
mSkbSample.setOnTouchListener(new OnTouchListener() {       
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            int progress = mSkbSample.getProgress();
    mTxvSeekBarValue.setText(String.valueOf(progress).toCharArray(), 0, String.valueOf(progress).length());   
        }
        else if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            int progress = mSkbSample.getProgress();
    mTxvSeekBarValue.setText(String.valueOf(progress).toCharArray(), 0, String.valueOf(progress).length());  
        }
        return false;
    }
});

您不需要任何额外的方法,并且 ACTION_UP 事件对结果没有重要影响。

Found a better way. Derived from the sample:

mTxvSeekBarValue = (TextView) this.findViewById(R.id.txvSeekBarValue);
mSkbSample = (SeekBar) this.findViewById(R.id.skbSample);
mSkbSample.setOnTouchListener(new OnTouchListener() {       
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            int progress = mSkbSample.getProgress();
    mTxvSeekBarValue.setText(String.valueOf(progress).toCharArray(), 0, String.valueOf(progress).length());   
        }
        else if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            int progress = mSkbSample.getProgress();
    mTxvSeekBarValue.setText(String.valueOf(progress).toCharArray(), 0, String.valueOf(progress).length());  
        }
        return false;
    }
});

There you don't need any additional method and the ACTION_UP event has no important effect on the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文