11

我需要在搜索栏的中心放置“起点”。
我该怎么做?

为两个值寻找 barr

4

3 回答 3

15

我遇到了同样的问题。感谢 Commonsware 指出正确的方向。我写了一个受 code.google.com/p/range-seek-bar 启发的课程来获得解决方案。

https://github.com/vashisthg/StartPointSeekBar

于 2014-04-02T09:42:55.190 回答
7

自定义SeekBar

如上所述,我可以实现我们自己的自定义搜索栏。

我尝试了以下方式,它对我有用。

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" 
  android:layout_margin="20dp">

 <com.example.customseekbar.CustomSeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"        
    android:max="100"
    android:progress="50"
    android:progressDrawable="@android:color/transparent"
    android:id="@+id/customSeekBar"
 />

</RelativeLayout>

MainActivity.java

package com.example.customseekbar;

import com.example.suricustomseekbar.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;

public class MainActivity extends Activity {

SeekBar mseekBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mseekBar = (SeekBar) findViewById(R.id.customSeekBar);
    mseekBar.setProgress(50);
     }
  }

CustomSeekBar.java

package com.example.customseekbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.SeekBar;

  public class CustomSeekBar extends SeekBar {

private Rect rect;
private Paint paint ;
private int seekbar_height; 

public CustomSeekBar(Context context) {
    super(context);

}

public CustomSeekBar(Context context, AttributeSet attrs) {

    super(context, attrs);
    rect = new Rect();
    paint = new Paint();
    seekbar_height = 6;
}

public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected synchronized void onDraw(Canvas canvas) {

    rect.set(0 + getThumbOffset(), 
            (getHeight() / 2) - (seekbar_height/2), 
            getWidth()- getThumbOffset(), 
            (getHeight() / 2) + (seekbar_height/2));

    paint.setColor(Color.GRAY);

    canvas.drawRect(rect, paint);



    if (this.getProgress() > 50) {


        rect.set(getWidth() / 2,
                (getHeight() / 2) - (seekbar_height/2), 
                getWidth() / 2 + (getWidth() / 100) * (getProgress() - 50),
                getHeight() / 2 + (seekbar_height/2));

        paint.setColor(Color.CYAN);
        canvas.drawRect(rect, paint);

    }

    if (this.getProgress() < 50) {

        rect.set(getWidth() / 2 - ((getWidth() / 100) * (50 - getProgress())),
                (getHeight() / 2) - (seekbar_height/2),
                 getWidth() / 2,
                 getHeight() / 2 + (seekbar_height/2));

        paint.setColor(Color.CYAN);
        canvas.drawRect(rect, paint);

    }

    super.onDraw(canvas);
    }
}
于 2014-04-27T13:47:52.033 回答
2

要从中间开始,你可以setProgress()用你总范围的一半的值跟注。

SeekBar在中间开始并结束(即,您的蓝线从中间向左或从中间向右),您需要创建自己的替代品SeekBar,或找到其他人这样做,因为它本身不支持SeekBar

于 2013-07-01T23:16:34.510 回答