android– 以编程方式添加时自定义视图不膨胀布局

android– 以编程方式添加时自定义视图不膨胀布局,第1张

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

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

我的自定义视图:

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>

解决方法:

我最终通过扩展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);  }}
总结

以上是内存溢出为你收集整理的android – 以编程方式添加时自定义视图不膨胀布局全部内容,希望文章能够帮你解决android – 以编程方式添加时自定义视图不膨胀布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1105699.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存