我们有一个 TextView,其中包含应用程序资源中的可绘制左侧和可绘制右侧。在某些设备上,例如
- 运行 Android 4.1.2 的索尼 Xperia L,
- 三星 Galaxy Young 或
- 三星 Galaxy SII
我们面临着一个奇怪的问题。可绘制对象将显示为半透明/透明。在其他设备上,例如
- 三星 Galaxy S4 (4.4.2) 或
- HTC ONE V (4.0.3)
绘图将正确显示。
这是布局定义的截图:
<TextView
android:id="@+id/button_current_warnings"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="3dp"
android:gravity="center_vertical"
android:background="@drawable/selector_classic_list_item"
android:layout_gravity="center_vertical"
android:drawablePadding="8dp"
android:drawableLeft="@drawable/ic_classic_current_warning_white"
android:drawableRight="@drawable/ic_classic_row_arrow"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:maxLines="2"
android:ellipsize="end"
android:text="@string/current_warnings"
android:textSize="?attr/warningGermanyBoxTextSize"
android:textColor="?attr/itemFavoriteTitleTextColor"/>
在 HTC One V 设备上,此布局看起来相当不错:
在 Sony Xperia L 上,由于 imageview 可绘制对象的透明度,布局被破坏。两个可绘制对象仅显示了一点点:
此外,如果我们将可绘制对象更改为不同的可绘制对象,则两种设备上的结果都很好:
宏达电:
索尼:
此外,我还附加了用于当前测试的两个可绘制对象(它们都是白色的,因此您在这里看不到任何内容):
错误显示的可绘制对象:
正确显示的可绘制对象:
有人遇到同样的问题或对我们有任何提示吗?
更新:在我的代码中,我仅在此 TextView 上设置了 ClickListener:
buttonGermany = returnView.findViewById(R.id.button_current_warnings);
buttonGermany.setVisibility(View.VISIBLE);
buttonGermany.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Do something
}
});
最佳答案
我仅在 Android 4.1.2 (Jelly Bean) 设备上遇到此问题。图标半透明呈现的问题与 4.1.2 中可绘制对象的缓存有关。就我而言,我也在可绘制选择器中使用了错误的 ic_classic_current_warning_white-drawable:
<selector xmlns:android="schemas.android.com/apk/res/android"; android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/ic_classic_current_warning_white"/>
<item android:state_pressed="true" android:drawable="@drawable/ic_classic_current_warning_white"/>
<item android:drawable="@drawable/ic_classic_current_warning_black"/>
</selector>
此选择器用于之前 Activity 中的按钮。如果我触摸这个按钮,就会出现上面的 View 。通过使用 android:exitFadeDuration
ic_classic_current_warning_white-drawable 在触摸我的按钮后淡出。但当新 Activity 打开时,drawable 缓存似乎保存了对 ic_classic_current_warning_white-drawable 的引用,其 alpha 值低于 100%。如果我在下一个 View 中再次使用此可绘制对象,它不会以 100% alpha 显示,而是以离开上一个 Activity 时的 alpha 值显示。
我当前的解决方案只是复制可绘制的 ic_classic_current_warning_white。其中一个将用作选择器。另一个副本将用于“静态”目的。
关于android - ImageView 在某些设备上显示 Drawable 半透明/透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25543593/