有時候遇到一些看起來很奇怪的問題。在這裡一併記錄一下。
1、ListView拖動反黑
List正常顯示,但是一拖動,整個背景就變成黑色。停止拖動後,顏色又變回正常。
解決方案:在Layout檔案中設定ListView的屬性:
android:scrollingCache="false"
2、ListView預設點擊樣式失效
ListView是有預設點擊樣式的當設定了Item的background為某種顏色或某張圖片時,預設的點擊樣式就會失效。
其實想清楚了,很簡單。ListView本來有多種狀態:平常的、選中的、點擊時的。如果設定了背景,那麼所有這些狀態下都是這一個背景了。所以點擊時沒有反應。
正確的設定方法是:
在Drawable的資料裡建一個xml檔案,例如名為style_list_item.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/btn_item_sel" /><item android:drawable="@drawable/btn_item_nor" /></selector>
再設定item的Background為@drawbale/style_list_item
3、listView中removeFooterView之後,列表無法再點擊。
本來想實現列表滑動到底部時自動載入內容,載入完所有內容後,去掉列表底部的“更多”按鈕。但在實際實現過程中發現,在onScroll方法裡removeFooterView後,列表不會再響應對Item的點擊,有時過一會兒又會恢複。如果設定Visible為Gone,列表的底部還是會佔一塊空間。在網上看到另外的朋友也遇到這個問題。大概是因為不能在onScroll方法裡做這件事情。
後來就將removeFooterView的動作改到了其它地方,不放在onScroll方法裡就正常了。
4、代碼中動態添加TableRow到LinearLayout中,TableRow不顯示
原始碼如下(摘自StackOverFlow網站,這個問題我也是在那裡得到的答案,超愛StackOverFlow的)
LinearLayout tabella = (LinearLayout) findViewById(R.id.tabella_contatori); for(int i =0; i<array_list.size(); i++){ TableRow row = new TableRow(getApplicationContext()); row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); TextView data = new TextView(getApplicationContext()); data.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.2f)); data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium); data.setTextColor(Color.BLACK); data.setBackgroundColor(Color.WHITE); data.setPadding(2, 0, 0, 0); data.setText("asd"); row.addView(data); tabella.addView(row); }}
這時,修改代碼如下之後,再運行起來,TableRow的內容就會展現出來了。
data.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
就是添加到TableRow中的元素,它們LayoutParam應該是TableRow類中的LayoutParams。