android: 自定义loading

1. 实现效果

2. 定义loading.xml (res/layout/loading.xml)

  所有的color需要自定义,下方的TextView可根据实际情况

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <RelativeLayout
        android:layout_width="200sp"
        android:layout_height="200sp"
        android:background="@color/loadingBack"
        android:layout_gravity="center">

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="80sp"
            android:layout_height="80sp"
            android:layout_centerInParent="true"
            android:indeterminate="true"
            android:indeterminateTint="@color/loading" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="140sp"
            android:text="Loading..."
            android:textColor="@color/loadingText"
            android:textSize="18sp" />

    </RelativeLayout>

</FrameLayout>

3. 定义attrs.xml (res/values/attrs.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="LoadLayout">
        <attr name="loadingView" format="reference" />
    </declare-styleable>
</resources>

4. 定义LoadingView工具类

public class LoadingView extends FrameLayout {

    private int loadingLayoutId;
    private View loadingView;

    public LoadingView(@NonNull Context context) {
        this(context, null);
    }

    public LoadingView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // R.styleable.LoadLayout 对应attrs.xml中 name="LoadLayout"
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadLayout);
        //  R.styleable.LoadLayout_loadingView  对应attrs.xml中 name="loadingView"
        loadingLayoutId = a.getResourceId(R.styleable.LoadLayout_loadingView, R.layout.loading);
        loadingView = LayoutInflater.from(getContext()).inflate(loadingLayoutId, null);
        a.recycle();
    }


    /**
     * 布局加载完成后隐藏所有View
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        for (int i = 0; i < getChildCount() - 1; i++) {
            getChildAt(i).setVisibility(GONE);
        }
    }

    public void showLoading() {
        //VISIBLE    0  可见
        if(loadingView != null){
            removeView(loadingView);
            loadingView.setVisibility(VISIBLE);
            addView(loadingView);
        }
    }

    public void hideLoading(){
        //不可见也不占用布局空间 8  不可见也不占用布局
        if(loadingView != null){
            removeView(loadingView);
            loadingView.setVisibility(GONE);
        }
    }

}

5. 自定义Loading的使用

在需要用到loading的页面添加LoadingVew,width和height可根据实际情况。

<com.xxx.xxx.LoadingView
        android:id="@+id/loading_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

 在Fragment或Activity中使用。

LoadingView loadingView = (LoadingView) getActivity().findViewById(R.id.loading_view);

//开启loading
loadingView.showLoading();

//关闭loading
loadingView.hideLoading();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值