Tch
Tch
Published on 2021-12-29 / 24 Visits
0
0

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();


Comment