如何使用Android自定义视图中的属性设置调整膨胀布局的大小?

时间:2018-03-22 08:46:31

标签: android android-layout android-custom-view

我正在通过从布局中扩充来创建自定义视图,如下所示:

CustomView.java

public class CustomView extends FrameLayout {
    public CustomView(@NonNull Context context) {
        this(context, null);
    }

    public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.custom_view, this, true);
    }
}

custom_view.xml

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

    ...

</FrameLayout>

我的问题是,当我在布局中使用此自定义视图时,会使用一些layout_width和layout_height值,但内部的内容不会使用新值调整大小。

1 个答案:

答案 0 :(得分:0)

当您的CustomView扩展FrameLayoutcustom_view.xml的根也是FrameLayout时,您可以使用merge

只需将custom_view.xml的根目录更改为<merge></merge>