一.Toast
1.默认
Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_LONG).show();
2.定义位置
Toast toastCenter = Toast.makeText(getApplicationContext(), "居中Toast", Toast.LENGTH_LONG);
toastCenter.setGravity(Gravity.CENTER, 0, 0);
toastCenter.show();
3.自定义图片,文字等
Toast toastCustom = new Toast(getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
View viewCustom = inflater.inflate(R.layout.layout_toast, null);
ImageView imageView = viewCustom.findViewById(R.id.iv_toast);
TextView textView = viewCustom.findViewById(R.id.tv_toast);
imageView.setImageResource(R.drawable.smile);
textView.setText("自定义Toast");
toastCustom.setView(viewCustom);
toastCustom.setDuration(Toast.LENGTH_LONG);
toastCustom.show();
4.使用自封装的Toast类
public class ToastUtil {
public static Toast mToast;
public static void showMsg(Context context, String msg) {
if (mToast == null) {
mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
} else {
mToast.setText(msg);
}
mToast.show();
}
}
ToastUtil.showMsg(ToastActivity.this, "包装过的Toast");
ProgressBar(以下为几种常见类型)
1.默认
<ProgressBar
android:id="@+id/pb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
5.0以后ProgressBar的style默认值为"@android:style/Widget.Material.ProgressBar"
2.水平进度条
.xml文件
<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
android:max="100"
android:progress="10"
android:secondaryProgress="30"/>
max表示为进度条最大值,progress为当前进度(第一进度),secondaryProgress为第二进度。
.java文件(核心代码)
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (mPb3.getProgress() < 100) {
handler.postDelayed(runnable, 500);
} else {
ToastUtil.showMsg(ProgressActivity.this, "加载完成");
}
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
mPb.setProgress(mPb.getProgress() + 5);
handler.sendEmptyMessage(0);
}
};
mPb = findViewById(R.id.pb);
mBtnStart = findViewById(R.id.btn_start);
mBtnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.sendEmptyMessage(0);
}
});
3.带图进度条
values>style.xml
<style name="MyProgressBar">
<item name="android:indeterminateDrawable">@drawable/bg_progress</item>
</stye>
drawable>bg_progress
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/loading"
android:pivotX="50%"
android:pivotY="50%">
</animated-rotate>
layout>progress.xml
<ProgressBar
android:id="@+id/pb6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/MyProgressBar"
android:layout_marginTop="10dp"/>
自定义Dialog
单独一章(后期新增)