UI组件之弹出组件

一.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

单独一章(后期新增)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值