安卓 - SeekBar 起始点

6
我在视图中有两个SeekBar,它们在布局中的声明方式相似:

<SeekBar android:id="@+id/select_price"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="5dp" />
<TextView android:id="@+id/max_price"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="0 VND"
          android:layout_gravity="center" />

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="20dp"
              android:orientation="horizontal">
    <ImageView android:layout_width="20dp"
               android:layout_height="20dp"
               android:src="@drawable/ic_distance"
               android:layout_marginRight="5dp" />
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="18sp"
              android:text="@string/max_distance" />
</LinearLayout>

<SeekBar android:id="@+id/select_distance"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="5dp"/>
<TextView android:id="@+id/distance"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="0 Km"
          android:layout_gravity="center" />

这里是我使用它们的代码:

SeekBar sb = (SeekBar) v.findViewById(R.id.select_price);
            sb.setMax(1000);
            sb.setProgress(10);
            sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    TextView text = (TextView) v.findViewById(R.id.max_price);
                    text.setText(Integer.toString(progress) + PlaceListActivity.this.getResources().getString(R.string.dong));
                    maxPrice = progress;
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

            });

            SeekBar seek = (SeekBar) v.findViewById(R.id.select_distance);
            seek.setMax(10);
            seek.setProgress(1);
            seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    TextView text = (TextView) v.findViewById(R.id.distance);
                    text.setText(Integer.toString(progress) + PlaceListActivity.this.getResources().getString(R.string.km));
                    maxDistance = progress;
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

            });

但是当我在我的设备上调试应用程序时,第一个 seekbar 是正常的,但另一个则变得奇怪,我不知道为什么。这是截图:

enter image description here

如你所见,第一个进度条的起点不是从起始点开始的。这是什么原因呢?我真的希望有人能帮助我解决这个问题。谢谢。
2个回答

10

如果您想让两个SeekBar都从开始位置开始,同时将价格和距离的最小值分别设定为10和1,请将它们都设置为零,并通过编程方式添加最小值,如下所示:

    SeekBar sb = (SeekBar) v.findViewById(R.id.select_price);
    sb.setMax(1000);
    sb.setProgress(0); // Set it to zero so it will start at the left-most edge
    sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress = progress + 10; // Add the minimum value (10)
            TextView text = (TextView) v.findViewById(R.id.max_price);
            text.setText(Integer.toString(progress) + PlaceListActivity.this.getResources().getString(R.string.dong));
            maxPrice = progress;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}

    });

    SeekBar seek = (SeekBar) v.findViewById(R.id.select_distance);
    seek.setMax(10);
    seek.setProgress(0); // Set it to zero so it will start at the left-most edge
    seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress = progress + 1; // Add the minimum value (1)
            TextView text = (TextView) v.findViewById(R.id.distance);
            text.setText(Integer.toString(progress) + PlaceListActivity.this.getResources().getString(R.string.km));
            maxDistance = progress;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}

    });

谢谢您用所有的解释和代码让它变得清晰明了! - huong

4

根据您的代码很明显 - 第一个SeekBar在1%(进度10,最大1000),第二个SeekBar在10%(进度1,最大10)。

因此,您的第二个SeekBar不在起点。只需调整值即可...


我恐怕还不明白如何进行调整。你能帮我吗? - huong
你的代码包含setMax()和setProgress()的值。调整这些值。 - SirKnigget

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接