Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

java - thumb textview is not aligned with the seekbar's thumb center

I've tried a few things, but I couldn't solve them.

enter image description here

does anyone know why this happens?

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<TextView
    android:id="@+id/tv_quota"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FF233845"
    android:textSize="15sp" />

<SeekBar
    android:id="@+id/sb_quota"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:thumb="@drawable/seeker" />

Custom Class

    public class CustomSeeker extends LinearLayout {

    private SeekBar sb_quota;
    private TextView tv_quota;
    private Context mContext;

    public CustomSeeker(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public CustomSeeker(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();

    }

    private void init() {
        inflate(mContext, R.layout.mdnslider, this);
        tv_quota = findViewById(R.id.tv_quota);
        sb_quota = findViewById(R.id.sb_quota);
        sb_quota.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
                tv_quota.setText("" + progress);
                tv_quota.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

If anyone knows how to solve it or has an idea, I will be very grateful, I have been trying to solve this for a few days. I've tried to apply margins to textview and seekbar too, but no good results.

question from:https://stackoverflow.com/questions/66053184/thumb-textview-is-not-aligned-with-the-seekbars-thumb-center

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...