2013-02-25 67 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

相关问题