[英]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
擴展FrameLayout
且custom_view.xml
的根也是FrameLayout
,您可以使用merge 。
只需將custom_view.xml
的根更改為<merge></merge>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.