課程
/移動(dòng)開發(fā)
/Android
/打造Android中的流式布局和熱門標(biāo)簽
不能夠動(dòng)態(tài)的添加view ,
2015-08-13
源自:打造Android中的流式布局和熱門標(biāo)簽 1-1
正在回答
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,即需動(dòng)態(tài)添加View的容器
b). ItemAdapter為Grid填充數(shù)據(jù)的輔助類
現(xiàn)象
正常
如果把grid.xml中GridView的代碼直接復(fù)制粘貼到main.xml中LinearLayout容器內(nèi)沒有任何問題,布局正常。
不正常
如上動(dòng)態(tài)添加android:layout_height="fill_parent"就失效,不管這里設(shè)置絕對(duì)數(shù)值如300dp也不行,GridView始終只顯示有Item的內(nèi)容,即容器內(nèi)的View無法完全填充LinearLayout父容器。?
解決代碼:
ly.addView(gridview,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
舉報(bào)
酷炫的熱門標(biāo)簽到底是怎么實(shí)現(xiàn)的呢,本教程會(huì)給你答案
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-01-30
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,即需動(dòng)態(tài)添加View的容器
b). ItemAdapter為Grid填充數(shù)據(jù)的輔助類
現(xiàn)象
正常
如果把grid.xml中GridView的代碼直接復(fù)制粘貼到main.xml中LinearLayout容器內(nèi)沒有任何問題,布局正常。
不正常
如上動(dòng)態(tài)添加android:layout_height="fill_parent"就失效,不管這里設(shè)置絕對(duì)數(shù)值如300dp也不行,GridView始終只顯示有Item的內(nèi)容,即容器內(nèi)的View無法完全填充LinearLayout父容器。?
解決代碼:
ly.addView(gridview,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));