English 中文(简体)
Android子:在使用添加物时,对父母的忽视 观点
原标题:Android: match_parent is ignored when using addView

In my application I m adding a view dynamically to the layout using addView.
The added view was inflated beforehand using

andorid:layout_width="match_parent"  

然而,在添加意见之后,使用该观点

parentView.addView(view, index);  

The view is addded but dosen t fill the parent s width
I guess this is because the calculation to the "match_parent" size is done beforehand, when the view is inflated, and therefore it has no reference of how to set the corect width
is this the case?
is there a way for me to tell the view to recalculate the layout params?

my Code:
activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.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://schemas.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);

求助热线:

问题回答

Ok, just add this code

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

页: 1

View view = findViewById(R.id.placeHolder);
view .setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//addView(view)

感谢Dmytro Danylyk。 如今,其工作是完美的。





相关问题
Very basic WPF layout question

How do I get this listbox to be maxiumum size, i.e. fill the group box its in. I ve tried width="auto" Height="auto", width="stretch" Height="stretch" annoyingly at the moment, it dynamicly sizes to ...

How to align the text to top of TextView?

How to align the text to top of a TextView? Equivalent Android API for Swings setInsets()? that is top of text should start be in (0,0) of TextView <?xml version="1.0" encoding="utf-8"?> <...

Centring a flow in Shoes

Can I center the contents of a flow in Shoes? I know that a paragraph can be centred like: para Centred paragrpah , :align=> center However, this does not work with flows: flow(:align=> ...

Overlaying with CSS

I want to load a website in an iframe, and overlay the website with some hints about the website that is shown. However, i got stuck at the point that the website has a variable passive white space at ...

How do you normally make a program look beautiful? [closed]

How can I make an Application look nice and not like an amateur pulled it together? I mean graphic-wise. Is there some sort of book you can read regarding beautiful program layouts, etc? I put this ...

热门标签