標籤:cal sse 拖動 create stop secondary sed item 布局
1、簡介
SeekBar繼承ProgressBar,相關屬性和三種不同狀態下的觸發方法:
<!--<SeekBar--> <!--android:layout_width="match_parent"--> <!--android:layout_height="wrap_content"--> <!--android:max="100"--> <!--android:progress="30"--> <!--android:id="@+id/seekbar"--> <!--android:thumb="@mipmap/ic_launcher_round"/>-->//設定滑塊的
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override//進度發生改變 public void onProgressChanged(SeekBar seekBar, int i, boolean b) { } @Override//按下 public void onStartTrackingTouch(SeekBar seekBar) { } @Override//放開 public void onStopTrackingTouch(SeekBar seekBar) { } });
2、簡單使用
這是一個簡單自訂的SeekBar,淺綠色是進度條顏色,紅色是二級進度條顏色,黃色是背景色!
滑塊的selector檔案:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/seekbar_thumb_pressed" /> <item android:state_pressed="false" android:drawable="@drawable/seekbar_thumb_normal"/></selector>
進度條顏色layer-list檔案:layer-list建立的圖形列表,也就是一個drawable 圖形。
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <solid android:color="#FFFFD042" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <solid android:color="#f23f21" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="#FF96E85D" /> </shape> </clip> </item></layer-list>
整體xml布局檔案:
<LinearLayout 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:gravity="center" android:orientation="vertical" 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=".LoginActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="當前進度值:30" android:id="@+id/textview"/> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekbar" android:progress="30" android:secondaryProgress="50" android:maxHeight="5dp" android:minHeight="5dp" android:progressDrawable="@drawable/sb_bar" android:thumb="@drawable/sb_thumb"/></LinearLayout>
Java檔案三種狀態事件的觸發:
public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // Set up the login form. SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar); final TextView textView = (TextView)findViewById(R.id.textview); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override//進度發生改變 public void onProgressChanged(SeekBar seekBar, int i, boolean b) { textView.setText("當前進度值:"+i); } @Override//按下 public void onStartTrackingTouch(SeekBar seekBar) { Toast.makeText(LoginActivity.this,"按下",Toast.LENGTH_SHORT).show(); } @Override//放開 public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(LoginActivity.this,"放開",Toast.LENGTH_SHORT).show(); } }); }}
Android基礎控制項SeekBar拖動條的使用