目录
1. ProgressBar(进度条)
1.常用属性
android:max 进度条的最大值
android:progress 进度条已完成进度值
android:progressDrawable 设置轨道对应的Drawable对象
android:indeterminate 如果设置成true,则进度条不精确显示进度
android:indeterminateDrawable 设置不显示进度的进度条的Drawable对象
android:indeterminateDuration 设置不精确显示进度的持续时间
android:secondaryProgress 二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过progress属性进行设置
2.对应的在Java中我们可调用下述方法
getMax(): 返回这个进度条的范围的上限
getProgress() :返回进度
getSecondaryProgress(): 返回次要进度
incrementProgressBy(int diff): 指定增加的进度
isIndeterminate(): 指示进度条是否在不确定模式下
setIndeterminate(boolean indeterminate): 设置不确定模式下
3.设置progressBar的样式
widget.ProgressBar.Horizontal
横向进度条(精确模式或模糊模式,这取决于Android:indeterminate)
widget.ProgressBar
中号的圆形进度条(模糊模式)
widget.ProgressBar.Small
小号的圆形进度条(模糊模式)
widget.ProgressBar.Large
大号的圆形进度条(模糊模式)
widget.ProgressBar.Inverse
中号的圆形进度条(模糊模式),该样式适用于亮色背景(如白色)
widget.ProgressBar.small.Inverse
小号的圆形进度条(模糊模式),该样式适用于亮色背景(如白色)
widget.ProgressBar.Large
大号的圆形进度条(模糊模式),该样式适用于亮色背景(如白色)
4.运用
1.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--系统提供的圆形进度条,一次是小中大-->
<!--小-->
<ProgressBar
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--中-->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--大-->
<ProgressBar
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--系统提供的水平进度条-->
<!--精确进度条-->
<ProgressBar
android:id="@+id/progress_1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"/>
<!--模糊进度条-->
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:indeterminate="true"/>
</LinearLayout>
2.MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int maxprogress;//最大进度值
private int currentprogress=0;//当前进度值
private Handler mHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case 0:
progressBar.setProgress(currentprogress);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=findViewById(R.id.progress_1);
maxprogress=progressBar.getMax();
}
protected void onStart(){
super.onStart();
//启动线程模拟加载
new Thread(){
public void run(){
while(true){
try{
for(int i=0;i<=100;i++){
Thread.sleep(1000);
currentprogress+=10;
if(currentprogress>maxprogress)
break;
mHandler.sendEmptyMessage(0);
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}.start();
}
}
5.效果展示
2.SeekBar(拖动条)
1.常用属性
android:max=“100” 滑动条的最大值
android:progress=“60” 滑动条的当前值
android:secondaryProgress=“70” 二级滑动条的进度
android:thumb=" " 滑块的drawable
2.SeekBar事件
SeekBar.OnSeekBarChangeListener 需要重写三个对应的方法
1.onProgressChanged; 进度发生改变时会触发
2.onStartTrackingTouch; 按住SeekBar时会触发
3.onStopTrackingTouck; 放开SeekBar时触发
3.SeekBar定制
1.创建seekbar.xml文件
2.编辑seekbar.xml文件——滑块状态
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@mipmap/a1"/>
<item
android:state_pressed="false"
android:drawable="@mipmap/a2"/>
</selector>
3.编辑sb_bar.xml文件——条形栏Bar
<?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="#FF9800"/>
</shape>
</item>
<item
android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#8F9393"/>
</shape>
</clip>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#FFEB3B"/>
</shape>
</clip>
</item>
</layer-list>
4.运用
1.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SeekBar_MainActivity">
<SeekBar
android:id="@+id/sb_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tx_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="30sp"/>
<SeekBar
android:id="@+id/sb_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="5dp"
android:minHeight="5dp"
android:progressDrawable="@drawable/sb_bar"
android:thumb="@drawable/seek_bar"/>
</LinearLayout>
2.Seekbar_MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class SeekBar_MainActivity extends AppCompatActivity {
private SeekBar seekBar1;
private SeekBar seekBar2;
private TextView textView;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seek_bar__main);
context=this;
seekBar1=findViewById(R.id.sb_1);
seekBar2=findViewById(R.id.sb_2);
textView=findViewById(R.id.tx_1);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//监测当前滑块滑动的进度值
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("当前进度值:"+progress+"/100");
}
//触碰时
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(context,"触碰SeekBar",Toast.LENGTH_SHORT).show();
}
//放开时
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(context,"放开SeekBar",Toast.LENGTH_SHORT).show();
}
});
seekBar2.setSecondaryProgress(20);
}
}
5.效果展示
3.ScrollView(滚动条)
ps:ScrollView控件中只能包含一个View或一个ViewGroup
scrollView.fullScroll(ScrollView.FOCUS_DOWN); 滚动到底部
scrollView.fullScroll(ScrollView.FOCUS_UP); 滚动到顶部
android:scrollbars=“none”; 隐藏滑块
1.设置滚动速度
继承ScrollView,然后重写一个public void fling(int velocityY)方法;
public void fling(int velocityY){
super.fling(velocityY/2); //速度变为原来的一半
}
2.运用
1.布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Scrollview_MainActivity">
<Button
android:id="@+id/btn_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="滚动到底部"
android:textSize="30sp"/>
<Button
android:id="@+id/btn_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="滚动到顶部"
android:textSize="30sp"/>
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/tx_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</ScrollView>
</LinearLayout>
2.Scrollview_MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class Scrollview_MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button button1;
private Button button2;
private TextView textView;
private ScrollView scrollview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollview__main);
bindViews();
}
private void bindViews(){
button1=findViewById(R.id.btn_down);
button2=findViewById(R.id.btn_up);
scrollview=findViewById(R.id.scroll);
textView=findViewById(R.id.tx_2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
StringBuffer sb=new StringBuffer();
for(int i=1;i<=100;i++){
sb.append("今天学习Android了吗?"+i+"\n");
}
textView.setText(sb.toString());
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_down:
scrollview.fullScroll(ScrollView.FOCUS_DOWN);
break;
case R.id.btn_up:
scrollview.fullScroll(ScrollView.FOCUS_UP);
break;
}
}
}
3.效果展示