用户界面View之ProgressBar
一个成功的创业者,三个因素,眼光、胸怀和实力。
本讲内容:ProgressBar 进度条 与SeekBar 进度条(ProgressBar的子类)
一、ProgressBar 显示风格(默认是标准环形进度条)
style="?android:attr/progressBarStyleLarge" 大环形进度条
style="?android:attr/progressBarStyleSmall" 小环形进度条
style="?android:attr/progressBarStyleHorizontal" 水平进度条
二、ProgressBar 的分类
1、可以精确显示进度(水平进度条)
2、不可以精确显示进度(环形进度条)
三、ProgressBar的关键属性
android:max="100" 设置最大显示进度
android:progress="50" 设置第一显示进度
android:secondaryProgress="80" 设置第二显示进度
android:indeterminate="true" 设置是否精确显示。注意:true表示不精确显示进度,false表示精确显示进度
四、ProgressBar的关键方法
setProgress(int) 设置第一进度
setSecondaryProgress(int) 设置第二进度
getProgress() 获取第一进度
getSecondaryProgress() 获取第二进度
incrementProgressBy(int) 增加或减少第一进度
incrementSecondaryProgressBy(int)增加或减少第二进度
getMax() 获取最大进度
五、Android 不显示标题栏和全屏的设置方法
1.在Manifest.xml中设置
不显示标题栏
android:theme="@android:style/Theme.NoTitleBar"
全屏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
2.在代码中实现
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
六、Android所有控件的可见属性
可以通过android:visibility进行指定,可选值有三种,visible、invisible和gone。visible表示控件是可见的(这个值是默认值),invisible表示控件是不可见的,但是它仍然占据着原来的位置和大小,可以理解成控件变成透明状态,gone表示控件不仅不可见,而且不再占用任何屏幕空间,我们还可以通过代码来设置控件的可见性,使用setVisibility()方法,可以传入View.VISIBLE、View.INVISIBLE和View.GONE三种值。
示例一:标题上ProgressBar的设置
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 启用窗口特征,启用带进度和不带进度的进度条requestWindowFeature(Window.FEATURE_PROGRESS);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.activity_main);// 分别显示两种进度条setProgressBarVisibility(true);setProgressBarIndeterminateVisibility(true);setProgress(5000);//默认MAX=10000}}
示例二:
下面是res/layout/activity_main.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:orientation="vertical" > <ProgressBar android:id="@+id/id_progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="40" android:secondaryProgress="70" /> <Button android:id="@+id/id_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="增加" /> <Button android:id="@+id/id_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="减少" /> <Button android:id="@+id/id_reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="重置" /> <TextView android:id="@+id/id_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/></LinearLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity implements OnClickListener{private ProgressBar progress;private Button add;private Button reduce;private Button reset;private TextView text;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();}/** * 初始化控件 */private void initViews() {progress=(ProgressBar) findViewById(R.id.id_progressBar);add=(Button) findViewById(R.id.id_add);reduce=(Button) findViewById(R.id.id_reduce);reset=(Button) findViewById(R.id.id_reset);text=(TextView) findViewById(R.id.id_text);add.setOnClickListener(this);reduce.setOnClickListener(this);reset.setOnClickListener(this);// 获取第一进度条的进度int first = progress.getProgress();// 获取第二进度条的进度int second = progress.getSecondaryProgress();// 获取进度条的最大进度int max = progress.getMax();text.setText("第一进度百分比:" + first*100/max + "% 第二进度百分比:" + second*100/max + "%");}/** * 按钮点击事件 */public void onClick(View v) {switch (v.getId()) {case R.id.id_add:// 增加第一进度和第二进度10个刻度progress.incrementProgressBy(10);progress.incrementSecondaryProgressBy(10);break;case R.id.id_reduce:// 减少第一进度和第二进度10个刻度progress.incrementProgressBy(-10);progress.incrementSecondaryProgressBy(-10);break;case R.id.id_reset:progress.setProgress(40);progress.setSecondaryProgress(70);break;}text.setText("第一进度百分比:" + progress.getProgress()*100/progress.getMax() + "% 第二进度百分比:" + progress.getSecondaryProgress()*100/progress.getMax() + "%");}}
示例三:进度条对话框
下面是res/layout/activity_main.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:orientation="vertical" > <Button android:id="@+id/id_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="进度条对话框" /></LinearLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity implements OnClickListener{private Button show;private ProgressDialog dialog;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);show=(Button) findViewById(R.id.id_show);show.setOnClickListener(this);}/** * 按钮点击事件 */public void onClick(View v) {switch (v.getId()) {case R.id.id_show:/** * 对话框显示风格 *///新建ProgressDialog对象dialog=new ProgressDialog(MainActivity.this);//设置图标dialog.setIcon(R.drawable.ic_launcher);//设置标题dialog.setTitle("阳江");//设置对话框里的文字信息dialog.setMessage("欢迎大家支持!");//设置进度条显示风格(默认是环形)dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);/** * 设定关于ProgressBar的一些属性 *///设定最大进度dialog.setMax(100);//设定初始化已经增长到的进度dialog.incrementProgressBy(50);//进度条是明确显示进度的dialog.setIndeterminate(false);/** * 设定一个确定按钮 */dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "HelloWorld", Toast.LENGTH_SHORT).show();}});//默认的就是true,为fales时,只能按确定才能退出弹出框。dialog.setCancelable(false);//显示ProgressDialogdialog.show();break;}}}
示例四:自定义进度条样式(通过查看原码修改)
下面是res/drawable/progress_bar_bg文件
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <solid android:color="#88000000"/> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#B9A4FF" android:centerColor="#C6B7FF" android:centerY="0.75" android:endColor="#C3B2FF" android:angle="270" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#57E8FF" android:centerColor="#74EBFF" android:centerY="0.75" android:endColor="#8EEFFF" android:angle="270" /> </shape> </clip> </item> </layer-list>
下面是res/layout/activity_main.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:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" style="@android:style/Widget.ProgressBar.Horizontal" android:progressDrawable="@drawable/progress_bar_bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="40" android:secondaryProgress="70" /></LinearLayout>android:progressDrawable="@drawable/progress_bar_bg" 复盖系统自带样式
style="@android:style/Widget.ProgressBar.Horizontal" 通过ctrl+左键查看系统自带样式原码
示例五:SeekBar
下面是res/layout/activity_main.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:orientation="vertical" > <SeekBar android:id="@+id/id_seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" /> <TextView android:id="@+id/id_tv1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/id_tv2" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity implements OnSeekBarChangeListener{private SeekBar seekBar;private TextView tv1;private TextView tv2;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);seekBar=(SeekBar) findViewById(R.id.id_seekBar);tv1=(TextView) findViewById(R.id.id_tv1);tv2=(TextView) findViewById(R.id.id_tv2);seekBar.setOnSeekBarChangeListener(this);}/** * 数值改变 */public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {tv1.setText("正在拖动");tv2.setText("当前数值:"+progress);}/** * 开始拖动 */public void onStartTrackingTouch(SeekBar seekBar) {tv1.setText("开始拖动");}/** * 停止拖动 */public void onStopTrackingTouch(SeekBar seekBar) {tv1.setText("停止拖动");}}
示例六:自定义SeekBar(通过查看原码修改)
下面是res/layout/activity_main.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:orientation="vertical" > <SeekBar android:id="@+id/id_seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:thumb="@drawable/thumb" android:max="100" android:progress="50" /> <!-- android:thumb:拖动标志 --> <!-- 通过系统自带的样式可查看原码style="@android:style/Widget.SeekBar" --> <TextView android:id="@+id/id_tv1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/id_tv2" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
下面是drawable/thumb_selector文件
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/select" android:state_pressed="true" android:state_window_focused="true"/> <item android:drawable="@drawable/select" android:state_focused="true" android:state_window_focused="true"/> <item android:drawable="@drawable/select" android:state_selected="true" android:state_window_focused="true"/> <item android:drawable="@drawable/normal"/></selector>
Take your time and enjoy it
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- Android(安卓)学习笔记 databinding简单使用:使用databinding在li
- android Dialog 各种Dialog 三个选项的、进度条的、单选的、多选
- eclipse 打开SDK manager报错 A folder failed to be renamed or
- Android(安卓)GridView 横向滚动 一行显示
- android通过代码判断手机是否root