简体   繁体   English

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

[英]How to resize inflated layout with attribute set in Android custom view?

I am creating a custom view by inflating from a layout as following: 我正在通过从布局放大来创建自定义视图,如下所示:

CustomView.java 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 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>

My problem is that when I use this custom view in the layout with some layout_width and layout_height values, the content inside does not resize with the new value. 我的问题是,当我在具有某些layout_width和layout_height值的布局中使用此自定义视图时,其中的内容不会使用新值调整大小。

As your CustomView extends FrameLayout and the root of custom_view.xml is also FrameLayout , you can use merge . 当您的CustomView扩展FrameLayoutcustom_view.xml的根也是FrameLayout ,您可以使用merge

Just change the root of custom_view.xml to <merge></merge> . 只需将custom_view.xml的根更改为<merge></merge>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM