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

问题描述 投票:1回答:1

我通过从布局膨胀创建自定义视图,如下所示:

custom view.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值,内部的内容不会使用新值调整大小。

android android-layout android-custom-view
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.