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.