編輯:關於 Android 程式設計
先來看看效果圖:

拖動時數值跟著顯示,停止拖動後數字依然可見。這裡用 FrameLayout 結合 SeekBar 來實現。
版面配置(Layout)
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools"> <RelativeLayout android:id="@+id/wrapper_seekbar_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/img_seekbar_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" /> <TextView android:id="@+id/txt_seekbar_indicated_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="#333333" android:textSize="@dimen/space_12" tools:text="100" /> </RelativeLayout> <RelativeLayout android:id="@+id/wrapper_seekbar" android:layout_width="wrap_content" android:layout_height="wrap_content"> <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> </merge>
如果想自訂,可以直接在這份佈局裡調整圖片、顏色等,或者乾脆封裝成一個獨立元件。
初始化函式
private void init(Context context, AttributeSet attrs, int defStyle) { View view = LayoutInflater.from(context).inflate( R.layout.view_seekbar_indicated, this); bindViews(view); if (attrs != null) setAttributes(context, attrs, defStyle); mSeekBar.setOnSeekBarChangeListener(this); mTextViewProgress.setText(String.valueOf(mSeekBar.getProgress())); getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onGlobalLayout() { mMeasuredWidth = mSeekBar.getWidth() - mSeekBar.getPaddingLeft() - mSeekBar.getPaddingRight(); mSeekBar.setPadding( mSeekBar.getPaddingLeft(), mSeekBar.getPaddingTop() + mWrapperIndicator.getHeight(), mSeekBar.getPaddingRight(), mSeekBar.getPaddingBottom()); setIndicator(); getViewTreeObserver() .removeOnGlobalLayoutListener(this); } }); // mWrapperIndicator.setVisibility(View.GONE); } 核心邏輯是依據進度是否改變以及是否正在觸控,來決定文字和指示圖的顯示:
@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { setIndicator(); if (mOnSeekBarChangeListener != null) mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser); } @Override public void onStartTrackingTouch(SeekBar seekBar) { if (mOnSeekBarChangeListener != null) { mOnSeekBarChangeListener.onStartTrackingTouch(seekBar); mWrapperIndicator.setVisibility(View.VISIBLE); } } @Override public void onStopTrackingTouch(SeekBar seekBar) { if (mOnSeekBarChangeListener != null) { mOnSeekBarChangeListener.onStopTrackingTouch(seekBar); mWrapperIndicator.setVisibility(View.GONE); } } 原理其實很簡單,就不再多贅述了。
專案地址:
https://github.com/xiaoli1993/SeekBarIndicated/tree/47ffcc890fb9c7000bb20d9b248620564c2c8122
以上就是本文的全部內容,希望對大家的學習有所幫助。
EventBus 3.0 原始碼解析:從註冊、發送到線程切換
前言在 Android 開發中,EventBus 常用於組件間通信。本文以 EventBus 3.0 為例,從取得實例、註冊訂閱者、發送事件到取消註冊,梳理其核心源碼脈絡
View 滑動沖突的成因與兩種解法——Android 開發藝術探索筆記
問題從哪裡來 做 Android 的人大概都有過這種經歷:從網上下載的 demo 跑得好好的,一旦嵌進自己的專案、外層多套了一層可滑動容器,滑動就開始「神經」——該橫滑的
Android 仿 iOS 風格的 ViewPager 滑動進度條
最近做專案時遇到這樣的需求:ViewPager 分頁時,若頁數為 6 頁(含 6 頁)就用圓點表示,若超過 6 頁則改用進度條來切換。前一種互動方式最為常見,用小圓點表示
如何在 Android 中實現懸浮且透明的 Activity
把一款已經打磨好的手機 App 移植到平板上,通常不算難事:Fragment 加上合理的模塊拆分,很快就能搞定。但最近接到的項目沒這麼輕鬆。除了要基於 ActionBar