2013-02-25 79 views
0

因爲我正在開發API級別7以上,所以setAlpha(float)對我無效。因此我已經實現了遍歷給定ViewGroup的所有子元素的方法,並試圖找到設置alpha的方法,以便元素幾乎是透明的。不幸的是,我不能想出一種方法來使listview及其項目透明。我如何繼續?如何設置列表視圖中所有項目的alpha值?

public void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled)  
{  
    int childCount = viewGroup.getChildCount();   
    for (int i = 0; i < childCount; i++) {   
     View view = viewGroup.getChildAt(i); 

     view.setEnabled(enabled);    
     if(!enabled) { 
      if(view instanceof TextView) { 
       int curColor = ((TextView) view).getCurrentTextColor();     
       int myColor = Color.argb(ALPHA_NUM, Color.red(curColor), 
        Color.green(curColor), 
        Color.blue(curColor)); 

       ((TextView)view).setTextColor(myColor); 
      } 
      else if(view instanceof ImageView) 
      { 
       ((ImageView) view).setAlpha(ALPHA_NUM); 
      } 
      else if(view instanceof ListView) 
      { 
       // How do I set the text color of the subitems in this listview? 
      } 
      else 
      { 
       try 
       { 
         Paint currentBackgroundPaint = ((PaintDrawable) view.getBackground()).getPaint(); 
         int curColor = currentBackgroundPaint.getColor(); 
         int myColor = Color.argb(ALPHA_NUM, Color.red(curColor), Color.green(curColor), Color.blue(curColor)); 
         view.setBackgroundColor(myColor); 
        }catch(NullPointerException e) 
        { 
         Log.d("viewNotFound", "View " + view.getId() + " not found.."); 
         e.getStackTrace(); 
        } 

       } 
       if (view instanceof ViewGroup) { 
        enableDisableViewGroup((ViewGroup) view, enabled); 
       }   }  } 
+0

請發佈您的lsitview的適配器代碼。您將希望能夠在適配器處理您的列表視圖子項時執行此類操作。 – petey 2013-02-25 17:20:23

+0

沒有什麼值得一看的。 listView.setAdapter(new ArrayAdapter (getActivity(),android.R.layout.simple_list_item_1,items)); – CodePrimate 2013-02-25 17:21:15

回答

0

是否可以直接在您的ListView行的XML中設置此選項?

例如,如果你的佈局是一個LinearLayout

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent"> 

    <!-- stuff --> 
</LinearLayout> 

我不知道您的設置,但你可以以編程方式設置它這樣,如果你真的想你ListView是透明的。

ListView lv = (ListView)view; 
lv.setBackgroundColor(android.R.color.tramnsparent); 

在這種情況下,你需要設置你的ListView行是在XML透明反正。

如果要控制行內的View的透明度,最好的辦法是創建一個自定義的ArrayAdapter並在那裏處理。

+0

listview的背景顏色將如何影響子項目中TextView的文本顏色? – CodePrimate 2013-02-25 18:25:04

+0

我想我很困惑你在找什麼。您是否可以將所有子項目的背景設置爲在XML中始終保持透明? – Kirk 2013-02-25 18:53:17

+0

我最終處理了在自定義適配器中設置我的TextViews的alpha值。 – CodePrimate 2013-02-25 19:15:57

相關問題