带标题可拖动的SeekBar

来源:互联网 发布:淘宝密码修改 编辑:程序博客网 时间:2024/05/22 10:41

# xml

<LinearLayout    android:id="@+id/seek_bar_layout"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical"    android:visibility="visible">    <FrameLayout        android:id="@+id/fram_layout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="2dp"        android:visibility="visible">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:paddingTop="5dp"            android:src="@drawable/linqu_slide_frame" />        <TextView            android:id="@+id/seek_bar_value"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:gravity="center"            android:singleLine="true"            android:text=""            android:textColor="@color/white" />    </FrameLayout>    <SeekBar        android:id="@+id/seek_bar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="5dp"        android:layout_marginRight="5dp"        android:layout_marginTop="5dp"        android:max="25"        android:padding="0dp"        android:progressDrawable="@drawable/linqu_sild_bg"        android:thumb="@drawable/linqu_slide_money"        android:visibility="visible" /></LinearLayout>

##代码 

ma_seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {    @Override    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {      Drawable drawable =  ma_seekBar.getThumb();        Rect rect =  drawable.getBounds();        ViewUtils.setVisible(fram_layout);        fram_layout.setPadding(rect.left,0,0,0);        try {            tipFee = progress;            seek_bar_tv.setText("小费" + progress + "元");            setMoney();            seek_bar_value.setText(progress + "");        } catch (Exception e) {            e.printStackTrace();        }    }    @Override    public void onStartTrackingTouch(SeekBar seekBar) {    }    @Override    public void onStopTrackingTouch(SeekBar seekBar) {    }});
这样就可以实现:


另外添加一个View获取大小的回调方法:

ma_seekBar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {    @Override    public void onGlobalLayout() {        Drawable drawable =  ma_seekBar.getThumb();        Rect rect =  drawable.getBounds();        fram_layout.setPadding(rect.left,0,0,0);        seek_bar_tv.setText("小费" + ma_seekBar.getProgress() + "元");        seek_bar_value.setText(ma_seekBar.getProgress() + "");    }});

这是我第一天写博客, 虽然有点粗鲁。  明天研究下markdown写博客



0 0
原创粉丝点击