以编程方式添加时,自定义视图不会使布局膨胀(Custom view not inflating layout when added programmatically)

编程入门 行业动态 更新时间:2024-10-28 20:24:20
以编程方式添加时,自定义视图不会使布局膨胀(Custom view not inflating layout when added programmatically)

我正在尝试以编程方式将自定义视图添加到线性布局。 我希望自定义视图膨胀我已经创建的布局。 当我创建自定义视图的实例并添加它时,一个点显示视图的位置,但布局似乎没有膨胀(将我的自定义布局的背景颜色设置为绿色,但我看不到空间中的绿色,也不是布局中的图像)。

我的自定义视图:

public class AddressView extends View { public AddressView(Context context) { super(context); View.inflate(context, R.layout.place_address, null); } public AddressView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); View.inflate(context, R.layout.place_address, null); } public AddressView(Context context, AttributeSet attrs) { super(context, attrs); View.inflate(context, R.layout.place_address, null); } }

我的自定义视图布局:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/address_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00ff00" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/house_1" /> </RelativeLayout>

我在活动中的实例化:

LinearLayout content = (LinearLayout) findViewById(R.id.content); AddressView addressView = new AddressView(this); LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 400); content.addView(addressView, 0, lp);

我的R.id.content(线性布局是scrollview的唯一子项):

<LinearLayout android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <FrameLayout android:id="@+id/thumbnail_container" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/thumbnail" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@drawable/test_house" /> </FrameLayout> </LinearLayout>

I'm trying to add a custom view to a linear layout programmatically. I want the custom view to inflate a layout I've already created. When I create an instance of the custom view and add it, a spot shows up where the view is, but the layout didn't seem to be inflated (set the background color of my custom layout to green, but I don't see green in the space, nor the image in the layout).

My custom view:

public class AddressView extends View { public AddressView(Context context) { super(context); View.inflate(context, R.layout.place_address, null); } public AddressView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); View.inflate(context, R.layout.place_address, null); } public AddressView(Context context, AttributeSet attrs) { super(context, attrs); View.inflate(context, R.layout.place_address, null); } }

My custom view layout:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/address_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00ff00" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/house_1" /> </RelativeLayout>

My instantiation in an activity:

LinearLayout content = (LinearLayout) findViewById(R.id.content); AddressView addressView = new AddressView(this); LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 400); content.addView(addressView, 0, lp);

My R.id.content (The linear layout is the only child of a scrollview):

<LinearLayout android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <FrameLayout android:id="@+id/thumbnail_container" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/thumbnail" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@drawable/test_house" /> </FrameLayout> </LinearLayout>

最满意答案

我最终通过扩展FrameLayout而不是View来解决这个问题,并将其传递给inflate调用。 我认为该视图正在被添加和膨胀,但它不知道如何正确布置子项,如果您最初将父项传递给inflate调用,则会解决该问题:

public class AddressView extends FrameLayout { public AddressView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.place_address, this); } public AddressView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); LayoutInflater.from(context).inflate(R.layout.place_address, this); } public AddressView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.place_address, this); } }

I eventually worked around this by extending FrameLayout instead of View, and passing this to the inflate call. I think the view was being added and inflating, but it didn't know how to lay out the children correctly, which is resolved if you pass the parent to the inflate call initially:

public class AddressView extends FrameLayout { public AddressView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.place_address, this); } public AddressView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); LayoutInflater.from(context).inflate(R.layout.place_address, this); } public AddressView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.place_address, this); } }

更多推荐

本文发布于:2023-07-28 23:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   会使   视图   布局   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!