2014-11-24 44 views
12

在我的應用我改變反彈時發光效果的顏色是這樣的:的Android棒棒糖滾動型邊緣效應顏色

int glowDrawableId = contexto.getResources().getIdentifier("overscroll_glow", "drawable", "android"); 
Drawable androidGlow = contexto.getResources().getDrawable(glowDrawableId); 
assert androidGlow != null; 
androidGlow.setColorFilter(getResources().getColor(R.color.MyColor), PorterDuff.Mode.SRC_ATOP); 

但是,當我更新棒棒糖該代碼崩潰。我得到以下錯誤代碼:

FATAL EXCEPTION: main 
Process: com.myproject.myapp, PID: 954 
android.content.res.Resources$NotFoundException: Resource ID #0x0 
at android.content.res.Resources.getValue(Resources.java:1233) 
at android.content.res.Resources.getDrawable(Resources.java:756) 
at android.content.res.Resources.getDrawable(Resources.java:724) 

似乎在棒棒糖中缺少overscroll_glow資源。 我如何在棒棒糖中實現這一點?

在此先感謝。

+1

一般而言,您不應該嘗試引用這樣的框架專用資源。它們不保證在操作系統更新或甚至同一操作系統的不同OEM定製版本之間持續存在。 – alanv 2014-11-24 18:20:34

回答

35

你可以在你的主題指定android:colorEdgeEffect到整個應用程序內改變反彈時發光顏色。默認情況下,這個繼承由android:colorPrimary設置的原色值。

RES /值/的themes.xml:

<style name="MyAppTheme" parent="..."> 
    ... 
    <item name="android:colorEdgeEffect">@color/my_color</item> 
</style> 

替代地,可以修改該值用於使用在線主題覆蓋的單一視圖。

RES /價值/的themes.xml:

<!-- Note that there is no parent style or additional attributes specified. --> 
<style name="MyEdgeOverlayTheme"> 
    <item name="android:colorEdgeEffect">@color/my_color</item> 
</style> 

RES /佈局/ my_layout.xml:

<ListView 
    ... 
    android:theme="@style/MyEdgeOverlayTheme" /> 
+0

我使用該方法得到以下eroor。錯誤:(6,21)找不到與給定名稱匹配的資源:attr'colorEdgeEffect'。 – user1506630 2014-12-05 14:50:00

+1

對不起,應該是android:colorEdgeEffect。 – alanv 2014-12-07 03:04:51

+1

@alanv你知道以編程方式更改輝光顏色的方法嗎?在我的應用程序中,我根據上下文以編程方式更改狀態欄和工具欄顏色,並且很好地更改overscroll發光顏色。我發現棒棒糖中的新聯繫人應用根據聯繫人而改變。 – MrBrightside 2014-12-12 13:08:27

1

overscroll_glow.png不會在平臺21存在的您可以從平臺20複製資源的開發和使用。

你可以找到overscroll_glow.png

{SDK_FOLDER}\platforms\android-20\data\res

這樣你不使用反射,可以,你可以更新一些後看到,與您的程序混亂。

4

在棒棒糖的反彈時效果顏色可以與項目風格colorPrimary定製:

<style name="MyApp" parent="Theme.AppCompat.Light"> 
    <item name="colorPrimary">@color/mycolor</item> 
</style> 

此產品也影響工具欄的顏色。

7

"android:colorEdgeEffect"解決方案完美的作品,而且是比以前的黑客要好得多。但是,如果邊緣顏色需要以編程方式更改,則無法使用。

這是可能,雖然使用反射來做到這一點,在AbsListViewScrollView情況下直接設置EdgeEffect對象。例如:

EdgeEffect edgeEffectTop = new EdgeEffect(this); 
edgeEffectTop.setColor(Color.RED); 

EdgeEffect edgeEffectBottom = new EdgeEffect(this); 
edgeEffectBottom.setColor(Color.RED); 

try { 
    Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop"); 
    f1.setAccessible(true); 
    f1.set(listView, edgeEffectTop); 

    Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom"); 
    f2.setAccessible(true); 
    f2.set(listView, edgeEffectBottom); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

EdgeEffect.setColor()已添加到棒棒糖中。

相同警告任何基於反射的溶液,雖然。

2

我使用這個以編程方式更改邊的顏色在Android L.這同時適用於ListView和滾動視圖,並享有擴展它們。

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
private static void setEdgeEffectL(View scrollableView, int color) { 
    final String[] edgeGlows = {"mEdgeGlowTop", "mEdgeGlowBottom", "mEdgeGlowLeft", "mEdgeGlowRight"}; 
    for (String edgeGlow : edgeGlows) { 
     Class<?> clazz = scrollableView.getClass(); 
     while (clazz != null) { 
      try { 
       final Field edgeGlowField = clazz.getDeclaredField(edgeGlow); 
       edgeGlowField.setAccessible(true); 
       final EdgeEffect edgeEffect = (EdgeEffect) edgeGlowField.get(scrollableView); 
       edgeEffect.setColor(color); 
       break; 
      } catch (Exception e) { 
       clazz = clazz.getSuperclass(); 
      } 
     } 
    } 
}