Android Dynamic Add view problem solving method _android

Source: Internet
Author: User

Background code

Copy Code code as follows:

private void ChangeView ()
{
Ly.removeallviews ();
Layoutinflater Inflater = (layoutinflater) getsystemservice (Layout_inflater_service);
View layout = inflater.inflate (r.layout.grid,null);
GridView GridView = (GridView) Layout.findviewbyid (R.id.gridview);
Gridview.setadapter (New Itemadapter (Mainactivity.this));
Gridview.setonitemselectedlistener (New Onitemselectedlistener () {

public void onitemselected (Adapterview arg0, View arg1,
int arg2, long arg3) {
}
public void onnothingselected (Adapterview arg0) {
}
});

Ly.addview (GridView);
}

Code Description:

a). Ly is the LinearLayout with ID contentview in Main.xml, the container that needs to dynamically add view

b). Itemadapter a secondary class that populates data for the grid

Phenomenon

Normal

If the code of the GridView in Grid.xml is copied and pasted directly into the Main.xml linearlayout container, there is no problem, the layout is normal.

Not normal

If the dynamic add android:layout_height= "Fill_parent" on the failure, regardless of the setting absolute value such as 300DP, the GridView will always show only the contents of the item, That is, the view inside the container cannot completely populate the LinearLayout parent container.

Third, solve the code


Just one line of code, don't know if Android bugs or what:

Copy Code code as follows:

Ly.addview (gridview,new linearlayout.layoutparams (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

End


This problem bothers me two hours +, no matter how to say or solve, happy ing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.