您的位置:首页 > 其它

带标题可拖动的SeekBar

2016-03-14 21:17 423 查看
# 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写博客
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: