Custom scrollable layout with default values

懵懂的女人 提交于 2019-12-13 04:23:54

问题


I'm trying to make custom RelativeLayout inside ScrollView.
Now I have to copy this part of code in every place where I want to use it:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="xxx"
        android:layout_height="yyy">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:layout_margin="@dimen/card_margin"
            android:background="@drawable/card_background"
            android:gravity="center"
            android:padding="@dimen/card_padding">

        // content
    </RelativeLayout>
</ScrollView>

I would like to get the same results with just this:

<MyScrollableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="xxx"
        android:layout_height="yyy">

    // content
</MyScrollableRelativeLayout>

Is this even possible to make it in this way?

Update 1

I've tried to make this in this way:

public class MyScrollableRelativeLayout extends ScrollView {
    private RelativeLayout content;
    int padding;
    int margin;

    public MyScrollableRelativeLayout(Context context) {
        super(context);
        postConstruct();
    }

    public MyScrollableRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        postConstruct();
    }

    public MyScrollableRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        postConstruct();
    }

    private void postConstruct() {
        float scale = getResources().getDisplayMetrics().density;
        padding = (int) (5 * scale);
        margin = (int) (20 * scale);

        content = new RelativeLayout(getContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(margin, margin, margin, margin);
        addView(content, layoutParams);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setLook();
    }

    private void setLook() {
        setCardBackground();
        setCardPadding();
    }

    private void setCardBackground() {
        content.setBackground(getResources().getDrawable(R.drawable.card_background));
    }

    private void setCardPadding() {
        setPadding(padding, padding, padding, padding);
    }

    @Override
    public void addView(@NonNull View child) {
        if (getChildCount() == 0) {
            super.addView(child);
        } else {
            content.addView(child);
        }
    }

    @Override
    public void addView(@NonNull View child, int index) {
        if (getChildCount() == 0) {
            super.addView(child, index);
        } else {
            content.addView(child, index);    }
    }

    @Override
    public void addView(@NonNull View child, ViewGroup.LayoutParams params) {
        if (getChildCount() == 0) {
            super.addView(child, params);
        } else {
            content.addView(child, params);
        }
    }

    @Override
    public void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {
        if (getChildCount() == 0) {
            super.addView(child, index, params);
        } else {
            content.addView(child, index, params);
        }
    }
}

But the problem in element within the ScrollableCardRelativeLayout are overriding themselves. Like they were ignoring android:layout_* attribute. What am I doing wrong?


回答1:


you can use some tricky wrapper of LayoutParams like this:

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class MyScrolledRelativeLayout extends ScrollView {
    private static final String TAG = "MyScrolledRelativeLayout";
    private RelativeLayout rl;

    public MyScrolledRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        rl = new RelativeLayout(context);
        addView(rl);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParamsWrapper(getContext(), attrs);
    }

    public void addView(View child, ViewGroup.LayoutParams params) {
        Log.d(TAG, "addView " + child + " " + params);
        if (getChildCount() != 0) {
            LayoutParamsWrapper layoutParamsWrapper = (LayoutParamsWrapper) params;
            rl.addView(child, layoutParamsWrapper.wrapped);
        }
    };

    class LayoutParamsWrapper extends FrameLayout.LayoutParams {
        private RelativeLayout.LayoutParams wrapped;
        public LayoutParamsWrapper(Context c, AttributeSet attrs) {
            super(c, attrs);
            wrapped = new RelativeLayout.LayoutParams(c, attrs);
        }
    }
}


来源:https://stackoverflow.com/questions/24680131/custom-scrollable-layout-with-default-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!