【Android】ProgressBar圆形加载样式、进度条样式以及代码控制显示隐藏
16lz
2021-01-26
一、预览
ProgressBar的使用很简单,先看一下效果图
二、layout布局
就放了两个progressBar和两个Button
<?xml version="1.0" encoding="utf-8"?><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_horizontal" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_marginTop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="圆形加载样式progressBar"/> 默认的样式是圆形加载样式 <ProgressBar android:id="@+id/progressBar1" android:layout_width="match_parent" android:visibility="gone" 先让它隐藏,代码控制显示 android:layout_height="wrap_content"/> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示"/> <TextView android:layout_marginTop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="进度条样式progressBar"/> <ProgressBar android:id="@+id/progressBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" 先让它隐藏,代码控制显示 style="?android:attr/progressBarStyleHorizontal" 设置为水平进度条样式 android:max="100" 设置进度条的最大值为100 /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示"/></LinearLayout>
三、代码控制progressBar的显示和隐藏
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn1,btn2; private ProgressBar progressBar1,progressBar2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); progressBar1 = findViewById(R.id.progressBar1); btn1.setOnClickListener(this); progressBar2 = findViewById(R.id.progressBar2); btn2.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ //显示圆形加载样式progressBar case R.id.btn1: //判断ProgressBar当前是否可见,并且设置其是否可见 //View.VISIBLE是显示,View.INVISIBLE是隐藏但占位置,View.GONE是消失不占位置 if (progressBar1.getVisibility()==View.GONE){ progressBar1.setVisibility(View.VISIBLE); }else { progressBar1.setVisibility(View.GONE); } break; //显示进度条样式progressBar case R.id.btn2: progressBar2.setVisibility(View.VISIBLE); int progress = progressBar2.getProgress();//获取当前进度 progress += 10; progressBar2.setProgress(progress); //重新设置进度 if(progress > 100){ progressBar2.setVisibility(View.GONE); } break; default: break; } }}
更多相关文章
- Android百度地图开发之显示当前位置地图
- android键盘谈起和收起监听
- Android实现随机圆形云标签效果
- Android中用BaseAdapter实现带Button和显示网络图片的Listview
- Android(安卓)常用基本控件 ImageButton
- onPrepareOptionsMenu 和onCreateOptionsMenu 的区别
- Android经常使用UI组件 - TextView
- android多图拼接长图并合理显示
- Android(安卓)中Webview 自适应屏幕