具有默认值的自定义可滚动布局

WojciechKo

我正在尝试在ScrollView中创建自定义RelativeLayout。
现在,我必须在要使用它的每个位置复制这部分代码:

<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>

我想这样得到相同的结果:

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

    // content
</MyScrollableRelativeLayout>

这样是否有可能做到这一点?

更新1

我试图以这种方式做到这一点:

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中的element中的问题已被自己覆盖。就像他们忽略了android:layout_*属性。我究竟做错了什么?

pskink

您可以使用一些棘手的LayoutParams包装器,如下所示:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在SQL查询中添加具有默认值的自定义列?

来自分类Dev

如何创建具有预定义的可见性,控件名称,默认值的Orbeon自定义控件XBL?

来自分类Dev

有什么方法可以在Kendo网格自定义弹出模板中设置默认值?

来自分类Dev

在哪里设置自定义控件默认值

来自分类Dev

从自定义首选项访问默认值

来自分类Dev

自定义标记扩展的默认值?

来自分类Dev

检查自定义列多项选择的默认值

来自分类Dev

C#自定义控件默认值属性

来自分类Dev

Inno Setup中的自定义命令行参数具有默认值,因此我可以通过构建配置

来自分类Dev

使用 EF Core FromSql 将存储过程结果映射到自定义类返回具有默认值的实例

来自分类Dev

python pandas dataframe:创建具有默认值的新列,当默认值是可迭代的

来自分类Dev

Django 3.x-自定义下拉菜单的自定义默认值

来自分类Dev

为具有自定义滚动功能的元素重新启用浏览器的默认滚动功能

来自分类Dev

具有溢出菜单的自定义操作栏布局

来自分类Dev

AlertDialog中的findViewById(具有自定义布局)-NullPointerException

来自分类Dev

具有子视图的自定义布局

来自分类Dev

具有布局的自定义错误页面

来自分类Dev

AlertDialog中的findViewById(具有自定义布局)-NullPointerException

来自分类Dev

具有隐藏字段的自定义布局的ListView?

来自分类Dev

具有溢出布局的UWP自定义控件

来自分类Dev

具有水平滚动的自定义视图

来自分类Dev

Magento,自定义模块的默认布局

来自分类Dev

具有自定义布局和自定义菜单项的ActionBar

来自分类Dev

自定义TabBarController包含可滚动的选项卡,并具有5个以上的选项卡

来自分类Dev

如何在Flutter中实现自定义应用栏布局可滚动效果

来自分类Dev

具有自定义对象列表的可包裹类

来自分类Dev

具有Symfony的可空自定义表单实体

来自分类Dev

自定义Keras层具有可训练的标量

来自分类Dev

具有自定义逻辑的可重用UITableViewCell

Related 相关文章

  1. 1

    如何在SQL查询中添加具有默认值的自定义列?

  2. 2

    如何创建具有预定义的可见性,控件名称,默认值的Orbeon自定义控件XBL?

  3. 3

    有什么方法可以在Kendo网格自定义弹出模板中设置默认值?

  4. 4

    在哪里设置自定义控件默认值

  5. 5

    从自定义首选项访问默认值

  6. 6

    自定义标记扩展的默认值?

  7. 7

    检查自定义列多项选择的默认值

  8. 8

    C#自定义控件默认值属性

  9. 9

    Inno Setup中的自定义命令行参数具有默认值,因此我可以通过构建配置

  10. 10

    使用 EF Core FromSql 将存储过程结果映射到自定义类返回具有默认值的实例

  11. 11

    python pandas dataframe:创建具有默认值的新列,当默认值是可迭代的

  12. 12

    Django 3.x-自定义下拉菜单的自定义默认值

  13. 13

    为具有自定义滚动功能的元素重新启用浏览器的默认滚动功能

  14. 14

    具有溢出菜单的自定义操作栏布局

  15. 15

    AlertDialog中的findViewById(具有自定义布局)-NullPointerException

  16. 16

    具有子视图的自定义布局

  17. 17

    具有布局的自定义错误页面

  18. 18

    AlertDialog中的findViewById(具有自定义布局)-NullPointerException

  19. 19

    具有隐藏字段的自定义布局的ListView?

  20. 20

    具有溢出布局的UWP自定义控件

  21. 21

    具有水平滚动的自定义视图

  22. 22

    Magento,自定义模块的默认布局

  23. 23

    具有自定义布局和自定义菜单项的ActionBar

  24. 24

    自定义TabBarController包含可滚动的选项卡,并具有5个以上的选项卡

  25. 25

    如何在Flutter中实现自定义应用栏布局可滚动效果

  26. 26

    具有自定义对象列表的可包裹类

  27. 27

    具有Symfony的可空自定义表单实体

  28. 28

    自定义Keras层具有可训练的标量

  29. 29

    具有自定义逻辑的可重用UITableViewCell

热门标签

归档