自定义滚动布局默认值 [英] Custom scrollable layout with default values

查看:183
本文介绍了自定义滚动布局默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让里面滚动型定制RelativeLayout的。结果
现在我要code的这一部分中,我想用它的每一个地方复制:

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?

我试图让这个是这样的:

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

但在ScrollableCardRelativeLayout内元素的问题,压倒一切的是他们自己。就像他们忽略了安卓布局_ * 属性。我在做什么错了?

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

推荐答案

您可以使用的LayoutParams的一些棘手的包装是这样的:

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

这篇关于自定义滚动布局默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆