51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Android: 自定义loading

1. 实现效果 {#1.-%E5%AE%9E%E7%8E%B0%E6%95%88%E6%9E%9C}


2. 定义loading.xml (res/layout/loading.xml) {#2.-%E5%AE%9A%E4%B9%89loading.xml-(res%2Flayout%2Floading.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">

    &lt;RelativeLayout
        android:layout_width="200sp"
        android:layout_height="200sp"
        android:background="@color/loadingBack"
        android:layout_gravity="center"&gt;

        &lt;ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="80sp"
            android:layout_height="80sp"
            android:layout_centerInParent="true"
            android:indeterminate="true"
            android:indeterminateTint="@color/loading" /&gt;

        &lt;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" /&gt;

    &lt;/RelativeLayout&gt;



`</FrameLayout>`


3. 定义attrs.xml (res/values/attrs.xml) {#3.-%E5%AE%9A%E4%B9%89attrs.xml-(res%2Fvalues%2Fattrs.xml)}

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

4. 定义LoadingView工具类 {#4.-%E5%AE%9A%E4%B9%89loadingview%E5%B7%A5%E5%85%B7%E7%B1%BB}

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 &lt; 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的使用 {#5.-%E8%87%AA%E5%AE%9A%E4%B9%89loading%E7%9A%84%E4%BD%BF%E7%94%A8}

在需要用到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)
未经允许不得转载:工具盒子 » Android: 自定义loading