Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android:使用addView时会忽略match_parent大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我使用addView动态地向布局添加视图.
添加的视图预先使用膨胀

andorid:layout_width="match_parent"

但是,在添加视图后使用

parentView.addView(view,indeX);

视图已添加,但不会填充父级的宽度
我想这是因为“match_parent”大小的计算是事先完成的,
当视图膨胀时,因此它没有参如何设置corect宽度
是这样的吗?
有没有办法告诉视图重新计算布局参数?

我的代码
activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://scheR_707_11845@as.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:BACkground="@color/blue_bg">
    <ImageView android:id="@+id/myImg"    
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:src="@drawable/myImg"
        android:scaleType="fitXY"/>
    <LinearLayout android:id="@+id/addressItemContainer" 
        android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:layouT_Below="@id/myImg" android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">
        <View android:id="@+id/placeHolder" 
            android:layout_width="match_parent" android:layout_height="match_parent"/>
    </LinearLayout>
</RelativeLayout>

inserted_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_707_11845@as.android.com/apk/res/android"    
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="wrap_content" android:BACkground="@drawable/blue_SELEctor">
.... internal views
</LinearLayout>

java代码

LayoutInflater li = (LayoutInflater)getSystemservice(Context.LAYOUT_INFLATER_serviCE);
View view = li.inflate(R.layout.inserted_view,null);
... fill inserted view fields ...
View aiv = findViewById(R.id.placeHolder);
ViewGroup parent = (ViewGroup) aiv.getParent();
int index = parent.indexOfChild(aiv);
parent.removeView(aiv);
parent.addView(view,indeX);

Tx的帮助:)

解决方法

好的,只需添加代码即可

View aiv = findViewById(R.id.placeHolder);
aiv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

马克已经解决了.

大佬总结

以上是大佬教程为你收集整理的Android:使用addView时会忽略match_parent全部内容,希望文章能够帮你解决Android:使用addView时会忽略match_parent所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。