簡體   English   中英

如何使用Android自定義視圖中設置的屬性調整充氣式布局的大小?

[英]How to resize inflated layout with attribute set in 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值的布局中使用此自定義視圖時,其中的內容不會使用新值調整大小。

當您的CustomView擴展FrameLayoutcustom_view.xml的根也是FrameLayout ,您可以使用merge

只需將custom_view.xml的根更改為<merge></merge>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM