Jos*_*arl 57 android image tint selector imagebutton
我有一个ImageButton在我的应用程序,我需要在按钮时更改图像的色调pressed/focused.我有一个从XML文件ImageButton获取它的集合,src如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item
android:state_pressed="true"
android:tint="@color/black"
android:drawable="@drawable/search"
/>
<!-- focused -->
<item
android:state_focused="true"
android:tint="@color/black"
android:drawable="@drawable/search"
/>
<!-- default -->
<item
android:tint="@null"
android:drawable="@drawable/search"
/>
</selector>
Run Code Online (Sandbox Code Playgroud)
但是,ImageButton按下或聚焦时不会应用色调- 图像只显示正常.黑色一直定义为黑色#000000.有任何想法吗?
Jef*_*eff 93
您可以通过以下方式在代码中轻松更改色调:
ImageButton button = (ImageButton) this.findViewById(R.id.button_i_want_to_modify);
button.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
JS
Ras*_*ria 18
以下是使用xml的方法.在drawable文件夹中创建一个选择器.例如:touch_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- State when a row is being pressed, but hasn't yet been activated (finger down) -->
<item android:state_pressed="true" android:color="@color/semi_slate" />
<!-- When the view is "activated". In SINGLE_CHOICE_MODE, it flags the active row
of a ListView -->
<item android:state_activated="true" android:color="@color/semi_slate" />
<!-- Default, "just hangin' out" state. -->
<item android:color="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)
在我的xml图像视图中,我将android:tint属性设置为上面创建的drawable.
android:tint = "@drawable/touch_selector"
Run Code Online (Sandbox Code Playgroud)
整个代码看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/poster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:tint="@drawable/touch_selector" />
Run Code Online (Sandbox Code Playgroud)
这是一个全xml解决方案,可以在印刷机上或活动时将色调放在ImageView上.类似的可以为ImageButton完成
请注意,这仅适用于API级别> = 21.
我找到了一种在xml中执行此操作的方法(至少在api 21及以上).
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<bitmap
android:src="@drawable/search"
android:tint="@color/black"
/>
</item>
<item android:drawable="@drawable/search"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
通过在位图上设置色调,可以在xml中重用相同的drawable,而不必截取触摸或子类ImageView或ImageButton.
创建选择器后,只需将其应用为ImageView或ImageButton的src即可.
最后我找到了API <21的解决方案:
Button more = (Button) findViewById(R.id.more);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
more.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
} else {
Drawable wrapDrawable = DrawableCompat.wrap(more.getBackground());
DrawableCompat.setTint(wrapDrawable, color));
more.setBackgroundDrawable(DrawableCompat.unwrap(wrapDrawable));
}
Run Code Online (Sandbox Code Playgroud)
愿这有帮助的人不要失去2个小时!
| 归档时间: |
|
| 查看次数: |
88067 次 |
| 最近记录: |