Android動態(tài)添加View的問題解決方法
后臺代碼
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);
}
代碼說明:
a). ly為main.xml中id為ContentView的LinearLayout,即需動態(tài)添加View的容器
b). ItemAdapter為Grid填充數據的輔助類
現象
正常
如果把grid.xml中GridView的代碼直接復制粘貼到main.xml中LinearLayout容器內沒有任何問題,布局正常。
不正常
如上動態(tài)添加android:layout_height="fill_parent"就失效,不管這里設置絕對數值如300dp也不行,GridView始終只顯示有Item的內容,即容器內的View無法完全填充LinearLayout父容器。
三、 解決代碼
就一行代碼,不知道是Android的Bug還是怎么:
ly.addView(gridview,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
結束
這個問題煩了我兩個小時+,不管怎么說還是解決了,開心ing。
相關文章
Android 使用ViewPager實現圖片左右循環(huán)滑動自動播放
這篇文章主要介紹了Android 使用ViewPager實現圖片左右循環(huán)滑動自動播放的相關資料,非常不錯,具有參考解決價值,需要的朋友可以參考下2016-08-08android中soap協議使用(ksoap調用webservice)
kSOAP是如何調用ebservice的呢,首先要使用SoapObject,這是一個高度抽象化的類,完成SOAP調用??梢哉{用它的addProperty方法填寫要調用的webservice方法的參數2014-02-02